nrvo - C++: should I explicitly use std::move() in a return statement to force a move? -
this question has answer here:
edit: not duplicate because question asks compiler's decision in o0.
it said here name return value optimization (nrvo) optimization many compiler support. must or nice-to-have optimization?
my situation is, want compile -o0
(i.e. no optimization), convenience of debugging, want nrvo enabled return statements return objects (say, vector). if nrvo not must, compiler won't in -o0
mode. in case, should prefer code:
std::vector<int> foo() { std::vector<int> v(100000,1); // object big.. return std::move(v); // explicitly move }
over below?
std::vector<int> foo() { std::vector<int> v(100000,1); return v; // copy or move? }
edit: compiler using gcc6, want code compiler-independent.
you should prefer
std::vector<int> foo() { std::vector<int> v(100000,1); return v; // move or nrvo }
over
std::vector<int> foo() { std::vector<int> v(100000,1); return std::move(v); // move }
the second snippet prevent nrvo, , in worst case both move construct.
Comments
Post a Comment