繁体   English   中英

C ++ 11是否保证return语句中的局部变量将被移动而不是复制?

[英]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();
}

collxvaluereturn A{ coll };

C ++ 11是否保证在f返回时将调用A(vector<int>&&)

C ++ 11不允许移动coll 当您return <identifier> ,它仅允许在return语句中进行隐式移动,其中<identifier>是局部变量的名称。 任何比这更复杂的表达都不会隐式移动。

而比这更复杂的表达不会经历任何形式的省略。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM