When passing an rvalue reference (Movable&& a
) to a function, Movable b(a)
somehow triggers the deleted copy constructor.
main.cpp: In function 'void func(Movable&&)': main.cpp:25:16: error: use of deleted function 'Movable::Movable(const Movable&)' Movable b(a);
but Movable b(std::move(a))
works. From reading
template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept;
I don't understand the difference between Movable&& a
and std::move(a)
. Below is a minimal working example.
#include<memory>
class Movable {
public:
Movable() = default;
Movable(const Movable&) = delete;
Movable& operator=(const Movable&) = delete;
Movable(Movable&& o) {}
};
void func(Movable&& a) {
Movable b(std::move(a)); // this works
// Movable b(a); // this doesn't compile
}
int main() {
Movable A;
func(std::move(A));
}