[英]Does C++11 guarantee a dying object will be moved rather than copied as an argument?
[英]Does C++11 guarantee the local variable in a return statement will be moved rather than copied?
#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();
}
是coll
的xvalue
在return A{ coll };
?
C ++ 11是否保证在f
返回时将调用A(vector<int>&&)
?
C ++ 11不允许移动coll
。 当您return <identifier>
,它仅允许在return
语句中进行隐式移动,其中<identifier>
是局部变量的名称。 任何比这更复杂的表达都不会隐式移动。
而比这更复杂的表达不会经历任何形式的省略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.