A recent question got me wondering about explicit copy constructors. Here is a sample code that I tried compiling under Visual Studio 2005 :
struct A
{
A() {}
explicit A(const A &) {}
};
// #1 > Compilation error (expected behavior)
A retByValue()
{
return A();
}
// #2 > Compiles just fine, but why ?
void passByValue(A a)
{
}
int main()
{
A a;
A b(a); // #3 > explicit copy construction : OK (expected behavior)
A c = a; // #4 > implicit copy construction : KO (expected behavior)
// Added after multiple comments : not an error according to VS 2005.
passByValue(a);
return 0;
}
Now for the questions :
- Is #2 allowed by the standard ? If it is, what is the relevant section describing this situation ?
- Do you known of any practical use for an explicit copy constructor ?
[EDIT] I just found a funny link on MSDN with the exact same situation, and a mysterious comment from the main function : "c is copied" (as if it was obvious). As pointed by Oli Charlesworth : gcc does not compile this code and I believe he's right not to.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…