简体   繁体   中英

Move a std::future to another std::future

I'm trying to move a std::future member of class to another member of same class.

I need to do that because if I want to call std::vector.push_back(MyClass), I need to delete the copy constructor with a simple reference and create another one with double ref because my class contain a std::future member. When i move the std::future, G++ says :

call to deleted constructor of 'std::future<int>'
_future(std::move(p_myclass._future));
note: 'future' has been explicitly marked deleted here
future(const future&) = delete

But for me, std::move returns a double ref and not a single. So I don't really know where this error comes from.

I tried the assignement operator = with double ref but doesnt work either.

class MyClass
{
private:
    std::future<int> _future;
public:
    MyClass(const MyClass&) = delete;
    MyClass(const MyClass&&);
    MyClass& operator=(MyClass &) = delete;
    MyClass& operator=(MyClass &&);
};
MyClass::MyClass(const MyClass &&p_myclass) : _future(std::move(p_myclass._future)) {};

MyClass& MyClass::operator=(MyClass &&p_myclass)
{
    this->_future = std::move(p_myclass._future);
    return (*this);
};

Of course my std::future is initialised on main constructor, but there is no need to paste it here. Both doesn't work. Thanks in advice.

p_myclass is const , and then so is p_myclass._future . That's why it can't be moved from; moving from an object requires modifying it. _future(std::move(p_myclass._future)) attempts to copy it instead, but of course std::future is not copyable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM