I am confused about Rvalue References
Consider class like this
class A {
public:
A() { std::cout << "A()" << std::endl; }
A(A &&other) {
std::cout << "A(A &&other)" << std::endl;
}
A(A const &other) {
std::cout << "(A const &other)" << std::endl;
}
void operator =(A &&other) {
std::cout << "operator =(A &&other)" << std::endl;
}
void operator =(A other) {
std::cout << "operator =(A other)" << std::endl;
}
};
void test(A&& a) {
std::cout << "test(A&& a)" << std::endl;
}
When creating variable like this
A a;
A&& b = std::move(a);
No constructor is called expect default when a
is initialised.
A b = std::move(a);
This way it calls move constructor, as soon as trying to initialise object from rvalue reference.
When we pass rvalue to function
test(std::move(a));
It calls move constructor too. What is the difference between passing rvalue reference to function compared to initialising rvalue reference variable?
And why do we need to create
void test(A&& a);
if
void test(A a);
still calls move constructor? Just to be able to implement different logic?
What essentially rvalue reference function parameter is?