#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是否保证A(vector<int>&&)
将在 f
时调用返回?
最佳答案
C++11 不允许 coll
从中 move 。它只允许 return
中的隐式 move 当你做 return <identifier>
时的陈述, 其中<identifier>
是局部变量的名称。任何比这更复杂的表达式都不会隐式 move 。
比这更复杂的表达式不会经历任何形式的省略。
关于c++ - C++11 是否保证 return 语句中的局部变量将被 move 而不是复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42443495/