Strange use of Copy Constructor of gcc version 2.95.1 19990816 (release)

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Sun Mar 5 02:02:00 GMT 2000


> So, since P(const P&) is not declared any more, it seems that the
> compiler does not allow now, what has happened without any warnings in
> the previous example.

Thanks for your bug report. It's not a bug in the compiler, but a bug
in your code. When you write 

    P p=&i;

you are asking for a copy-initialization, whereas 

    P p(&i);

would have been a direct-initialization. In a copy-initialization, a
temporary of type P is created, which is then used to initialize p,
using a copy constructor. Since the only available copy constructor is

    P(P& p);

the program is ill-formed - you cannot bind a temporary (an r-value)
to a non-const referece.

If you question this line of reasoning, please discuss it in one of
the public C++ fora first, eg. comp.lang.c++.moderated, or
comp.std.c++.

Regards,
Martin


More information about the Gcc-bugs mailing list