#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
是return A{ coll };
xvalue
吗?
C++11 是否保证A(vector<int>&&)
f
回归时会被调用?
C++11 不允许从中移动coll
。它只允许在return
语句中隐式移动,当你执行return <identifier>
时,其中<identifier>
是局部变量的名称。任何比这更复杂的表达式都不会隐式移动。
而比这更复杂的表达式不会经历任何形式的省略。