I have the following piece of code:
#include <iostream>
struct T {
int a;
T() = default;
T(T& other) {
std::cout << "copy &\n";
}
T(T&& other) {
std::cout << "move &&\n";
}
};
void foo(T&& x) {
T y(x); // why is copy ctor called??????
}
int main() {
T x;
foo(std::move(x));
return 0;
}
I don't understand why copy constructor is preferred over move constructor even though foo()
accepts rvalue-reference.
x
is an lvalue itself, even its type is rvalue-reference. Value category and type are two independent properties.
Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression;
You need to use std::move
to convert it to rvalue, just same as using std::move
on x
in main()
.
void foo(T&& x)
{
T y(std::move(x));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With