Topic: Layout compatibility not clear


Author: "msalters" <Michiel.Salters@logicacmg.com>
Date: Fri, 15 Jul 2005 12:05:25 CST
Raw View

Maciej Sobczak schreef:
> Hello,
>
> Consider:
>
> class C
> {
> public:
>      char a;
> public:
>      char b;
> };
>
> struct S
> {
>      char a;
>      char b;
> };
>
> a) 9.2/12 says that members of C are not guaranteed to be allocated in
> any order (they have intervening access specifier). Obviously, members
> of S have guaranteed order of allocation.
>
> b) 9/4 says that C and S are POD.
>
> c) 9.2/14 says that two POD-structs are layout compatible if they have
> the same number and order of layout-compatible members.
>
> Question: Is the order of allocation of members in C guaranteed or not?

Yes, because of 9/4 and 9.2/14. 9.2/12 simply doesn't apply. Basically,
if a general rule allows an implementation to do X (and Y and Z), and
another special rule forbids X, then it's forbidden to do so. In this
case, X is reordering members. PODs are the special case, with extra
order constraints.

Special rules like "Undefined Behavior" can overrule other rules, but
that doesn't apply here.

HTH,
Michiel Salters.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: no.spam@no.spam.com (Maciej Sobczak)
Date: Wed, 13 Jul 2005 15:27:38 GMT
Raw View
Hello,

Consider:

class C
{
public:
     char a;
public:
     char b;
};

struct S
{
     char a;
     char b;
};

a) 9.2/12 says that members of C are not guaranteed to be allocated in
any order (they have intervening access specifier). Obviously, members
of S have guaranteed order of allocation.

b) 9/4 says that C and S are POD.

c) 9.2/14 says that two POD-structs are layout compatible if they have
the same number and order of layout-compatible members.

Question: Is the order of allocation of members in C guaranteed or not?


--
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]