为什么调用此复制构造函数而不是移动构造函数
Why is this copy constructor called rather than the move constructor?
以下代码片段导致在我期望调用移动构造函数的位置调用复制构造函数:
#include <cstdio>
struct Foo
{
Foo() { puts("Foo gets built!"); }
Foo(const Foo& foo) { puts("Foo gets copied!"); }
Foo(Foo&& foo) { puts("Foo gets moved!"); }
};
struct Bar { Foo foo; };
Bar Meow() { Bar bar; return bar; }
int main() { Bar bar(Meow()); }
在VS11 Beta上,在调试模式下,这将打印:
Foo gets built!
Foo gets copied!
Foo gets copied!
我检查了标准,Bar
似乎满足自动生成默认移动构造函数的所有要求,但这似乎不会发生,除非有其他原因导致无法移动对象。我在这里看到了很多与移动和复制构造函数相关的问题,但我认为没有人遇到过这个特定问题。
关于这里发生了什么的任何指示?这是标准行为吗?
不幸的是,VS11 不提供默认的移动构造函数。 请参阅"备注"部分中的"移动语义" - 引用:
与默认的复制构造函数不同,编译器不提供 默认移动构造函数。
相关文章:
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么调用复制构造函数而不是移动构造函数?
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 具有已删除移动和复制构造函数的类的就地构造
- 移动构造函数和右值引用
- 使用移动调用对等构造函数unique_ptr默认构造函数
- 为什么 std::memmove 中联合的默认非平凡移动构造函数C++?
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- 构造函数采用std::string_view与std::string并移动
- C++:为什么不调用移动构造函数?
- 了解构造函数在移动、复制、赋值语义中的行为
- 没有移动的构造函数移动课程
- 引用绑定和复制构造函数/移动构造函数
- 构造函数移动
- C++ 向量实现 - 移动构造函数 - 移动与前进
- 我真的必须取消移动构造函数/移动结构中的所有成员还是只是指针
- 将类(没有默认构造函数)移动到另一个类的move构造函数中