この投稿に関連して、この動作について説明してください。
#include <stdio.h>
struct B { B(B&) { } B() { } };
struct A {
template<typename T>
A(T&){ printf("A(T&)\n"); }
A() { }
// B b; // when this is uncommented, output changes
int i;
};
int main() {
A a;
A b(a);
// B b; commented:
// template wins:
// A<A>(A&) -- specialization
// A(A const&); -- implicit copy constructor
// (prefer less qualification)
// B b; uncommented:
// implicit copy constructor wins:
// A<A>(A&) -- specialization
// A(A&); -- implicit copy constructor
// (prefer non-template)
printf("\nA\n");
A const a1;
A b1(a1);
// B b; commented:
// implicit copy constructor wins:
// A(A const&) -- specialization
// A(A const&) -- implicit copy constructor
// (prefer non-template)
// B b; uncommented:
// template wins:
// A(A const&) -- specialization
// (implicit copy constructor not viable)
}
B b の場合、出力が変化します。はコメント解除されています。
どうやら、暗黙のコピーコンストラクターは、コメントが解除されたときにからA(A const&)
に変更されます。なんで?コピー コンストラクタに変更すると、 に戻ります。これで、コンパイラは、の仮パラメータが?になることに満足しています。これは規格と関係がありますか?(gcc 4.2.4 を使用しています。)A(A &)
B b;
B(B&){}
B(const B&){}
A(A const&)
A()
const