Why is this copy constructor called rather than the move constructor?(为什么调用这个复制构造函数而不是移动构造函数?)
问题描述
以下代码片段导致在我期望调用移动构造函数的地方调用复制构造函数:
The following code snippet causes the copy constructor to be called where I expected the move constructor to be called:
#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 上,在调试模式下,打印:
On VS11 Beta, in debug mode, this prints:
Foo gets built!
Foo gets copied!
Foo gets copied!
我检查了标准,Bar 似乎满足自动生成默认移动构造函数的所有要求,但这似乎不会发生,除非有另一个原因导致无法移动对象.我在这里看到了很多与移动和复制构造函数相关的问题,但我认为没有人遇到过这个具体问题.
I checked the standard and Bar seems to meet all requirements to have a default move constructor automatically generated, yet that doesn't seem to happen unless there's another reason why the object cannot be moved. I've seen a lot of move and copy constructor related questions around here but I don't think anyone has had this specific issue.
关于这里发生了什么的任何指示?这是标准行为吗?
Any pointers on what's going on here? Is this standard behaviour?
推荐答案
不幸的是,VS11 没有提供默认的移动构造函数.请参阅备注部分中的移动语义- 引用:
Unfortunately, VS11 doesn't provide a default move constructor. See Move Semantics in the Remarks section - to quote:
与默认的复制构造函数不同,编译器不提供默认移动构造函数.
Unlike the default copy constructor, the compiler does not provide a default move constructor.
这篇关于为什么调用这个复制构造函数而不是移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么调用这个复制构造函数而不是移动构造函数?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
