From: itaj sherman on
Does the standard specify whether two most derived objects of the same
type have the same layout?

Considering the following note from the standard 10.0.5:
Note: A base class subobject might have a layout (3.7) different from
the layout of a most derived object of
the same type.

It does suggest so. Otherwise they would say "two objects of same type
might have different layout".

itaj

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bart van Ingen Schenau on
On Feb 4, 10:34 pm, itaj sherman <itajsher...(a)gmail.com> wrote:
> Does the standard specify whether two most derived objects of the same
> type have the same layout?

I am not sure where it is specified, but to my knowledge, layout is a
property of the (most-derived) type. That means two objects of the
same most-derived type will always have the same layout.

>
> Considering the following note from the standard 10.0.5:
> Note: A base class subobject might have a layout (3.7) different from
> the layout of a most derived object of
> the same type.

That can happen due to the presence of virtual base classes.
For example:

class Base {};
class Intermediate1: public virtual Base {};
class Intermediate2: public virtual Base {};
class Derived: public Intermediate1, public Intermediate2 {};

Intermediate1 i1;
Intermediate2 i2;
Derived d;

The objects i1 and i2 will have a layout such that they both
physically contain a Base sub-object.
As the object d only contains one Base sub-object, at least one of the
Intermediate1 or the Intermediate2 sub-objects can not physically
contain the (shared) Base sub-object. This leads to a difference in
object layout depending on if it is a base-class sub-object or not.

>
> It does suggest so. Otherwise they would say "two objects of same type
> might have different layout".
>
> itaj
>
Bart v Ingen Schenau


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: achp on
On 5 фев, 00:34, itaj sherman <itajsher...(a)gmail.com> wrote:
> Does the standard specify whether two most derived objects of the same
> type have the same layout?

3.9/11 says that any type is "layout-compatible" to itself. It is
quite clear what it means for a POD type.

But do non-POD types have layout at all? Is it possible at all legally
observe any properties which describe the layout of a non-POD type? I
cannot think of any: byte-by-byte access and offsetof on non-POD
objects are both outlawed.

Therefore I think that, from the point of view of pure language, the
questions about the layout of non-POD types are rather philosophical.
Just like the question about the existence of god(s): physics cannot
study what cannot be observed.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
achp wrote:
> On 5 фев, 00:34, itaj sherman <itajsher...(a)gmail.com> wrote:
>> Does the standard specify whether two most derived objects of the same
>> type have the same layout?
>
> 3.9/11 says that any type is "layout-compatible" to itself. It is
> quite clear what it means for a POD type.
>
> But do non-POD types have layout at all? Is it possible at all legally
> observe any properties which describe the layout of a non-POD type? I
> cannot think of any: byte-by-byte access and offsetof on non-POD
> objects are both outlawed.
>
> Therefore I think that, from the point of view of pure language, the
> questions about the layout of non-POD types are rather philosophical.
> Just like the question about the existence of god(s): physics cannot
> study what cannot be observed.
>
>

Well you can compare the addresses of members of an instance of a class.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: achp on
On 10 фев, 22:29, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> Well you can compare the addresses of members of an instance of a class.

Yes, you can, but the only thing you can learn from comparison is
whether member m1 comes before or after m2. If the class declaration
contains an access label between them you are not even guaranteed that
such a comparison will yield consistent results on all instances of
the class...


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]