Does C ++ 11 save a local variable in the return statement, and not copy it?

#include <vector> using namespace std; struct A { A(const vector<int>&) {} A(vector<int>&&) {} }; A f() { vector<int> coll; return A{ coll }; // Which constructor of A will be called as per C++11? } int main() { f(); } 

Is coll a xvalue in return A{ coll }; ?

Is a C ++ 11 A(vector<int>&&) warranty provided on returning f ?

+7
c ++ c ++ 11 rvalue-reference move-semantics rvo
source share
1 answer

C ++ 11 does not allow moving coll . It allows implicit moves in return when you execute return <identifier> , where <identifier> is the name of a local variable. Any expression that is more complex than this will not be implicitly moved.

And expressions more complex than this will not undergo any form.

+11
source share

All Articles