このコードは VS2010 ではコンパイルできません。エラー C2440: 'argument' : cannot convert from 'A' to 'A &' が発生しますが、標準の 12.8p2 によれば、A::A(A&)
は有効なコピー コンストラクターであり、式ina
の左辺値です。A b = foo(a);
main()
#include <iostream>
class A
{
public:
int x;
A(int a) { x = a; std::cout << "Constructor\n"; }
A(A& other) { x = other.x; std::cout << "Copy ctor\n"; }
A(A&& other) { x = other.x; other.x = 0; std::cout << "Move ctor\n"; }
};
A foo(A a)
{
return a;
}
int main(void)
{
A a(5);
A b = foo(a);
}