Why do not C++11's move constructor/assignment operator act as expected
Posted
by
xmllmx
on Stack Overflow
See other posts from Stack Overflow
or by xmllmx
Published on 2012-09-28T21:11:06Z
Indexed on
2012/09/28
21:37 UTC
Read the original article
Hit count: 294
#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;
}
My expected output should be:
A()
A(A&&)
~A()
~A()
However, the actual output is: (The C++ compiler is: Visual Studio 2012)
A()
~A()
~A()
Is this a bug of VC++? or just my misunderstanding?
© Stack Overflow or respective owner