thomas said:
what's the difference between these two statements?
1.
class A{};
A temp(b);
This won't compile as you try to initialize A with a ctor that would
take a single argument and you have no such ctor defined.
Initialization as if you would have written instead "A a(temp);"
(notice that this is true ONLY because temp is the same type as A, if
temp were another type then "A a = temp;" would mean something
different).
In this case the compiler is free to either:
- create a temporary A(b) and then use the copy ctor to initialize "a" from it
- directly initialize a from "b" like you would have written "A a(b);"
However, in BOTH cases the compiler still has to require a copy ctor
of the form "A(A const&)".
the first works as expected
for the 2nd, compiler complains that there's no function A:A(A).
Is it my any chance that "A" is auto_ptr? (or a similar type that has
"A(A&)" copy ctor but does not have "A(A const&)" version?) Because
then it can be explained why the first compiles while the other
doesn't. If you have a "A(A&)" only copy ctor then in the first case
you are using that because you initialize "a" from a "A" lvalue (and a
reference "A&" can be bound to it). In the second case however you try
to initialize "a" from a temporary (even as I said if the compiler
doesn't use the temporary and copy ctor and directly tries to
initialize "a" from "b" it still requires a "A(A const&)" copy ctor
"as if" it would have created a temporary). And because you can't bind
reference to non const (ie "A&") to temporaries then you get the
error.
I don't get it. IMHO, they should be the same.
Your code sample is incorrect and incomplete, I'm just guessing here.