Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create bn::optional<T> instance when T contains const member variable #9

Closed
copyrat90 opened this issue Jun 27, 2021 · 2 comments

Comments

@copyrat90
Copy link
Contributor

copyrat90 commented Jun 27, 2021

If I understand correctly, bn::optional<T>::emplace() is the only option to create a bn::optional<T> object which contains actual value, when T contains const member variable(s).
std::optional<T>::emplace() supports this, but bn::optional<T>::emplace() doesn't.
Because bn::optional<T>::emplace() uses operator= internally, but T can't have assignment operators, as it has const member variable(s).

bn::optional

#include "bn_core.h"
#include "bn_log.h"
#include "bn_optional.h"

class Test
{
    const int num_;
public:
    Test(int num) : num_(num) {}
    int GetNum() const { return num_; }
};

int main()
{
    bn::core::init();

    bn::optional<Test> a;
    // error: use of deleted function 'Test& Test::operator=(Test&&)'
    a.emplace(42);
    BN_LOG(a->GetNum());

    while (true)
        bn::core::update();
}

std::optional

#include <iostream>
#include <optional>

class Test
{
    const int num_;
public:
    Test(int num) : num_(num) {}
    int GetNum() const { return num_; }
};

int main()
{
    std::optional<Test> a;
    // no issue
    a.emplace(42);
    std::cout << a->GetNum();
    return 0;
}
@GValiente
Copy link
Owner

Please check the latest commit, I think it is fixed now.

@copyrat90
Copy link
Contributor Author

It is fixed now.
Thank you for your quick action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants