#include <iostream>
using namespace std;
struct A
{
A()
{
cout << "A()" << endl;
}
~A()
{
cout << "~A()" << endl;
}
A(A&&)
{
cout << "A(A&&)" << endl;
}
A& operator =(A&&)
{
cout << "A& operator =(A&&)" << endl;
return *this;
}
};
struct B
{
// According to the C++11, the move ctor/assignment operator
// should be implicitly declared and defined. The move ctor
// /assignment operator should implicitly call class A's move
// ctor/assignment operator to move member a.
A a;
};
B f()
{
B b;
// The compiler knows b is a temporary object, so implicitly
// defined move ctor/assignment operator of class B should be
// called here. Which will cause A's move ctor is called.
return b;
}
int main()
{
f();
return 0;
}
我的预期输出应该是:
A()
A(A&&)
~A()
~A()
然而,实际输出是:(C++编译器是:Visual Studio 2012)
A()
~A()
~A()
这是VC++的bug吗?还是只是我的误会?
根据这篇博文 http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx,VC++ 2012目前实现N2844 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2844.html + DR1138 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1138, 但不是N3053 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3053.html。结果,编译器是not为您隐式生成移动构造函数或赋值运算符。如果添加显式默认值并将构造函数移动到B
然后你会得到你期望的输出。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)