struct A {};
struct B
{
B (A* pA) {}
B& operator = (A* pA) { return *this; }
};
template
struct Wrap
{
T *x;
operator T* () { return
It's because you're using "copy initialization". If you write the
declaration of oB
:
B oB(a);
, it should work. The semantics of the two initializations are
different. For B oB(a)
, the compiler tries to find a constructor
which can be called with the given arguments. In this case, B::B(A*)
can be called, because there is an implicite conversion from Wrap
to A*
. For B oB = a
, the semantics are to implicitly convert a
to
type B
, then use the copy constructor of B
to initialize oB
. (The
actual copy can be optimized out, but the legality of the program is
determined as if it weren't.) And there is no implicit conversion of
Wrap
to B
, only of Wrap
to A*
.
The assignment works, of course, because the assignment operator also
takes a A*
, and so the implicit conversion comes into play.