c++ - 为什么调用这个复制构造函数而不是移动构造函数?

标签 c++ visual-c++ c++11 copy-constructor 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 上,在 Debug模式下,打印:

Foo gets built!
Foo gets copied!
Foo gets copied!

我检查了标准,Bar 似乎满足自动生成默认移动构造函数的所有要求,但这似乎不会发生,除非有其他原因导致无法移动对象。我在这里看到了很多与移动和复制构造函数相关的问题,但我认为没有人遇到过这个具体问题。

关于这里发生了什么的任何指示?这是标准行为吗?

最佳答案

不幸的是,VS11 不提供默认的移动构造函数。参见 Move Semantics在备注部分 - 引用:

Unlike the default copy constructor, the compiler does not provide a default move constructor.

关于c++ - 为什么调用这个复制构造函数而不是移动构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10201659/

相关文章:

c++ - 未调用基类构造函数?

c++ - 使用 Cereal 库序列化 Eigen::Matrix

c++ - 你能通过保证多个线程不会访问同一个内存来避免锁定吗?

c++ - Mongoose 网络服务器获取当前工作线程

c++ - Visual C++ 可以检查不匹配的原型(prototype)和实现吗?

c++ - 在 Visual C++ 中单击按钮显示和隐藏图形

c++ - 模拟 std::move() 函数以评估其性能影响

c++ - 为什么 C++ 中没有对所有 C++ 标准库类型特征的语言支持?

c++ - 在光线追踪器中计算阴影时出现意外结果

c++ - DPDK 应用程序可以用 C++ 编写吗?如果可以,是如何完成的?