Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-01-24 09:12:42


"John Maddock" <john_at_[hidden]> writes:

>> The documentation says: "If types T1 and T2 are the same type, then
>> there is only one version of the single-argument constructor, and this
>> constructor initialises both values in the pair to the passed value."
>>
>> There is no indication that only one object of type T1 will actually
>> ever be created when T1 == T2 and are empty, even though that is in
>> fact the case. This violates a reasonable expectation (given by the
>> standard) that two objects of the same type will have distinct
>> addresses. The difference *is* detectable by the user.
>
> This occurs in other situations as well; in fact it's an intrinsic part of
> compressed_pair that the addresses returned by first() and second() may be
> the same if *either* T1 or T2 are empty classes, that's the whole point of
> the object.

That's fine and allowed by the language, most of the time. What's not
allowed is that two distinct objects *of the same type* are allocated
at the same address.

> The reason for doing things that way was that most compilers do not enable
> the empty base optimisation when multiply inheriting (and we can't inherit
> from the same class twice),

Yes, I'm intimately familiar with the issues.

> however this does have the side effect that the constructor and
> destructor for that type are called only once and not twice, off
> hand I can't think of any situation where this would actually cause
> a problem in practice. This could be "fixed" (and probably should
> be) by making one object instance a base class, and the other a data
> member, but they will still have the same address.

Not if they are the same type (or contain a common base or member at
the same offset), unless the compiler is buggy.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk