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();
}
coll
是return A{ coll };
xvalue
吗?
C++11 是否保证A(vector<int>&&)
f
回归时会被调用?
C++11 不允许从中移动coll
。它只允许在return
语句中隐式移动,当你执行return <identifier>
时,其中<identifier>
是局部变量的名称。任何比这更复杂的表达式都不会隐式移动。
而比这更复杂的表达式不会经历任何形式的省略。
相关文章:
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 使lambda不可复制/不可移动
- 复制和交换习惯用法与移动操作之间的交互
- 为什么调用复制构造函数而不是移动构造函数?
- 隐式移动与复制操作和遏制
- 具有已删除移动和复制构造函数的类的就地构造
- 使用移动和复制语义时函数匹配如何工作?
- 直接初始化不可复制、不可移动的成员,而不使用聚合初始化
- 包装二进制缓冲区 - 可能没有多余的移动/复制?
- 当使用三元运算符并删除移动/复制CTOR时,Visual Studio不执行RVO
- 我们是否应该从派生类调用基类移动复制/分配构造函数
- 具有unique_ptr成员变量的 C++ 移动/复制/赋值<AbstractClass>
- 返回带有已删除移动/复制 ctor 的类型临时
- 消除默认/删除移动/复制语义中涉及的样板的好方法是什么
- C++11:移动/复制构造不明确
- 关于移动/复制c++对象实例
- 我丢失了一个移动/复制赋值
- 如何创建一个不可移动/复制构造的对象数组
- 编译器何时在C++中移动/复制