EDIT: it is NOT a duplicate because this question asks about a compiler's decision in O0.
It is said here that Name Return Value Optimization (NRVO) is an optimization many compiler support. But is it a must or just a nice-to-have optimization?
My situation is, I want to compile with -O0
(i.e. no optimization), for convenience of debugging, but I also want NRVO to be enabled for return statements that return objects (say, a vector). If NRVO is not a must, the compiler probably won't do it in -O0
mode. In this case, should I prefer this code:
std::vector<int> foo() {
std::vector<int> v(100000,1); // an object that is really big..
return std::move(v); // explicitly move
}
over this below?
std::vector<int> foo() {
std::vector<int> v(100000,1);
return v; // copy or move?
}
EDIT: the compiler I am using is GCC6, but I want the code to be compiler-independent.
move
is superfluous as the value is already an rvalue (xvalue), and it prohibits copy-elison. So it's just a pessimization overall. Also, don't optimize your unoptimized builds. – Purism