Ten kod nie jest kompilowany w VS2010. Występuje błąd C2440: "argument": nie można przekonwertować z "A" na "A &", ale zgodnie z 12.8p2 w standardzie, A::A(A&)
jest prawidłowym konstruktorem kopii, a a
jest lwartością w wyrażeniu A b = foo(a);
w main()
.Czy ten kod nie powinien być kompilowany zgodnie z 12.8p2 w standardzie?
#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);
}
kompiluje i działa poprawnie w GCC 4.7.2. – interjay
Czy wywołuje on ctor ruchu, aby skopiować wartość zwracaną z foo() do b? – Belloc
Tak, patrz tutaj: http://ideone.com/Ykwm2Z – interjay