xml*_*lmx 8 c++ visual-c++ move-semantics c++11 visual-studio-2012
#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;
}
Run Code Online (Sandbox Code Playgroud)
我的预期输出应该是:
A()
A(A&&)
~A()
~A()
Run Code Online (Sandbox Code Playgroud)
但是,实际输出是:( C++编译器是:Visual Studio 2012)
A()
~A()
~A()
Run Code Online (Sandbox Code Playgroud)
这是VC++的错误吗?还是只是我的误会?
Visual C++ 2012没有为rvalue引用和移动操作实现最终的C++ 11规范(规范在标准化过程中多次更改).您可以在rvalue引用下的Visual C++团队博客文章"Visual C++ 11中的C++ 11功能"中找到更多信息.
在您的示例中,具体来说,这表现在两个方面:
用户定义的移动操作的定义A
不会抑制隐式声明的复制操作.
没有隐式定义的移动操作B
.
归档时间: |
|
查看次数: |
3063 次 |
最近记录: |