Reputation: 55
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.
Upvotes: 2
Views: 907
Reputation: 52471
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.
Upvotes: 3