此代码无法在 VS2010 中编译。它发出错误 C2440: 'argument' : cannot convert from 'A' to 'A &',但根据标准中的 12.8p2,A::A(A&)
它是一个有效的复制构造函数,并且a
是表达式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);
}