I saw the following words in the C++ standard draft N4582:
[over.best.ics/4] However, if the target is
(4.1) the first parameter of a constructor or
(4.2) the implicit object parameter of a user-defined conversion function
and the constructor or user-defined conversion function is a candidate by
(4.3) 13.3.1.3, when the argument is the temporary in the second step of a class copy-initialization, or
(4.4) 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases),
user-defined conversion sequences are not considered.
I am confused about the bold part, and don't know how to understand it. I write the following program:
#include <iostream>
using namespace std;
struct A {
A(int) {}
operator int() {cout << "user-defined conversion" << endl; return 0;}
A(A&) {} //prevent default copy
};
int main()
{
A a = A(0);
}
It works well in g++ 5.3.0, and output "user-defined conversion", which means a user-defined conversion occurs. Certainly, it can be interpreted as that the temporary A(0) is not a consequence of copy-initialization. Next I change the program to:
#include <iostream>
using namespace std;
struct A {
A(int) {}
operator int() {cout << "user-defined conversion" << endl; return 0;}
A(A&) {} //prevent default copy
};
A foo() {return A(0);}
int main()
{
A a = foo();
}
Now the value of foo() is a temporary copy-initialized from A(0), but the program still works. Why would this happen?