8

以下代码片段导致在我期望调用移动构造函数的位置调用复制构造函数:

#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似乎满足了自动生成默认移动构造函数的所有要求,但除非有其他原因导致无法移动对象,否则这似乎不会发生。我在这里看到了很多与移动和复制构造函数相关的问题,但我认为没有人遇到过这个特定问题。

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

4

1 回答 1

8

不幸的是,VS11 没有提供默认的移动构造函数。请参阅备注部分中的移动语义- 引用:

与默认的复制构造函数不同,编译器不提供默认的移动构造函数。

于 2012-04-18T01:53:41.600 回答