Ten program nie kompiluje używając clang++ test.cpp -std=c++0x
:błąd kompilacji podczas wywoływania funkcji przeciążony ruch z niejawnie wymienialnej obiektu
class A
{
public:
A() {}
A(const A&) {}
A(A&&) {}
A& operator = (const A&) { return *this; }
A& operator = (A&&) { return *this; }
};
class B
{
A m_a;
public:
operator const A &() const
{
return m_a;
}
};
int main(int, char**)
{
A a;
B b;
a = b; // compile error
}
błędy kompilacji:
Apple clang version 3.0 (tags/Apple/clang-211.10.1) (based on LLVM 3.0svn)
test.cpp:25:9: error: no viable conversion from 'B' to 'A'
a = b;
^
test.cpp:5:5: note: candidate constructor not viable: no known conversion from 'B' to
'const A &' for 1st argument
A(const A&) {}
^
test.cpp:6:5: note: candidate constructor not viable: no known conversion from 'B' to 'A &&'
for 1st argument
A(A&&) {}
^
test.cpp:15:5: note: candidate function
operator const A &() const
^
test.cpp:8:23: note: passing argument to parameter here
A& operator = (A&&) { return *this; }
^
Dlaczego nie to skompilować? Dlaczego kompilator preferuje A::operator = (A&&)
przez A::operator = (const A&)
?
Ponadto dlaczego A a = b;
kompiluje się, podczas gdy zarówno A a; a = b;
(powyższy program) i A a(b);
nie?
Jaką wersję Clang? – ildjarn
FWIW, twój kod kompiluje się tak jak z 'clang version 3.0 (tags/RELEASE_30/final) Target: x86_64-pc-linux-gnu Modele wątku: posix' i GCC 4.5.3 lub 4.6.2 (ale mam nie mam pojęcia, czy to normalne czy nie) – Mat
Wygląda na to, że robisz błąd. –