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{ a1 };
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.
const, didn't know this before. Maybe you can elaborate abit about theboundingas you mentioned in an answer. How does it work? - rowmanf(T v)byf(const T &v)to eliminate the copy at each call. - Jean-Baptiste Yunès