Let's say I have a (trivial) class, which is move-constructible and move-assignable but not copy-constructable or copy-assignable:
class movable
{
public:
explicit movable(int) {}
movable(movable&&) {}
movable& operator=(movable&&) { return *this; }
movable(const movable&) = delete;
movable& operator=(const movable&) = delete;
};
This works fine:
movable m1(movable(17));
This, of course, does not work, because m1
is not an rvalue:
movable m2(m1);
But, I can wrap m1
in std::move
, which casts it to an rvalue-reference, to make it work:
movable m2(std::move(m1));
So far, so good. Now, let's say I have a (equally trivial) container class, which holds a single value:
template <typename T>
class container
{
public:
explicit container(T&& value) : value_(value) {}
private:
T value_;
};
This, however, does not work:
container<movable> c(movable(17));
The compiler (I've tried clang 4.0 and g++ 4.7.2) complains that I'm trying to use movable
's deleted copy-constructor in container
's initialization list. Again, wrapping value
in std::move
makes it work:
explicit container(T&& value) : value_(std::move(value)) {}
But why is std::move
needed in this case? Isn't value
already of type movable&&
? How is value_(value)
different from movable m1(movable(42))
?