From: xman on
Hi all,

I accidentally found that g++ compiles the following:

B b = b;

In this case, B is a class, and the copy constructor of B is called,
no default constructor is called however. It means that I'm actually
initializing b with an uninitialized b. Is this a compiler bug or
intended behavior? I thought copy constructor suppose to assume the
source is properly constructed before the copy.

Regards,
Shin Yee
From: Alf P. Steinbach on
* xman:
> Hi all,
>
> I accidentally found that g++ compiles the following:
>
> B b = b;
>
> In this case, B is a class, and the copy constructor of B is called,
> no default constructor is called however. It means that I'm actually
> initializing b with an uninitialized b. Is this a compiler bug or
> intended behavior? I thought copy constructor suppose to assume the
> source is properly constructed before the copy.

It's just Undefined Behavior.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Ulrich Eckhardt on
xman wrote:
> I accidentally found that g++ compiles the following:
>
> B b = b;
>
> In this case, B is a class, and the copy constructor of B is called,
> no default constructor is called however. It means that I'm actually
> initializing b with an uninitialized b.

Right.

> Is this a compiler bug or intended behavior?

Neither. The compiler behaves as it should, but I guess that g++ could warn
you that you are using an object before it is completely initialised (maybe
you should turn on warnings?). The bug is actually in your code, which
shouldn't use 'b' before it is constructed, which causes "undefined
behaviour". UB is the standard term for broken code where the standard
makes no requirements on the behaviour of the implementation.

Uli