struct A {};
struct B
{
B (A* pA) {}
B& operator = (A* pA) { return *this; }
};
template
struct Wrap
{
T *x;
operator T* () { return
The problem is that the number of user-defined conversions that are invoked implicitly is limited (to 1) by the Standard.
B ob = a;
implies two user conversions:
a
: Wrap::operator A*()
should be calledB::B(A*)
should be called@James Kanze's explanation: this syntax is called "copy initialization", effectively equivalent to B ob = B(a)
(with the copy being elided most of the time). This is different from B ob(a)
which is a "direct initialization" and would have worked.
if you explicitly qualify any of this, it will work, for example:
B ob = B(a);
On the other hand, for the second case there is no issue:
ob = a;
is short-hand for:
ob.operator=(a);
And thus only one user-defined conversion is required, which is allowed.
EDIT:
Since it's been required in a comment (to Kirill's answer) we can take a guess at the motive.
Chained conversions could be long, very long, and therefore:
Furthermore, as long as there is more than 1 conversion, you run into the risk of having cycles, which would have to be detected (even though diagnostic would probably not be required, and be subject to Quality Of Implementation).
So, since a limit is necessary to avoid infinitely long searches (it could have been left unspecified, with a minimum required), and since beyond 1 we may have new issues (cycles), then 1 seems as good a limit as any after all.