MWE:
struct A {
A() {std::cout << "constructor" << std::endl; }
A(const A& a) {std::cout << "copy constructor" << std::endl; }
A(A&& a) {std::cout << "move constructor" << std::endl; }
};
int main() {
A a1{};
A a2{ a2 };
A a3{ A{} };
A a4{ std::move(a3) };
return 0;
}
Output:
constructor
copy constructor
constructor
move constructor
fora2
copy elision is used which is an optimization of the compiler and everything seems to be fine. When I comment out the move constructor however, copy constructor is called in place of move constructor. How can an rvalue be converted to a const lvalue reference? Output:
constructor
copy constructor
constructor
copy constructor
The program is compiled in VS2017
.
Aucun commentaire:
Enregistrer un commentaire