I learned about return value optimization (Object return in C++, http://en.wikipedia.org/wiki/Return_value_optimization, http://blog.knatten.org/2011/08/26/dont-be-afraid-of-returning-by-value-know-the-return-value-optimization/) that prevents temporary object generation.
I also learned about rvalue reference (http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html) that also can be used to prevent temporary object generation.
Practically, can I just return value not worrying about performance degradation from copying objects?
I mean, are those two code snippet equivalent?
A hello()
{
A a(20);
cout << &a << endl;
return a;
}
// rvalue reference to prevent temporary object creation
A&& a = hello();
cout << &a << endl;
// expects compiler remove the temporary object
A a = hello();
cout << &a << endl;