?
=?ISO-8859-1?Q?Emmanuel_Thom=E9?=
This is a comment aside the empty class behavior FAQ.
I understand there are a fair number of reasons which make empty
classes have non-zero size (except as base classes). If ``class foo''
were of size 0, then:
- an object of type foo may lie at the same address as another.
- arrays of foo objects would have size 0, and pointer arithmetic on
array cell would break.
- requirements of the standard explicitly stating objects have
non-zero size would break (1.7.4 and 5.3.3.2, to start with).
(see also links below).
Now, I claim that even considering this, not having provision for
_really_ empty classes is a fiasco of the standard. The so-called
"empty member c++ optimization" (see links) is not a complete cure: it
relies on the availability of a non-empty member on which empty
classes are aggregated. Such a member is not always available. In
template metaprogramming world, one would prize the ability to create
possibly empty classes made of several possibly all empty members.
Extending the language, say via a keyword, to include ``ghost''
classes (by lack of a better name), which really occupy zero storage
when they are empty and present as members of anything would be, IMHO,
a very valuable addition. Implications would include:
- distinct objects of two non-ghost types would still reside at
distinct addresses. In case one of the object is a ghost class, this
would no longer hold.
- pointer arithmetic on ghost class pointers would be illegal.
Does such a thing make sense to anybody ?
Some relevant links include:
http://www.research.att.com/~bs/bs_faq2.html#sizeof-empty
http://www.cantrip.org/emptyopt.html
http://tinyurl.com/36r26
I understand there are a fair number of reasons which make empty
classes have non-zero size (except as base classes). If ``class foo''
were of size 0, then:
- an object of type foo may lie at the same address as another.
- arrays of foo objects would have size 0, and pointer arithmetic on
array cell would break.
- requirements of the standard explicitly stating objects have
non-zero size would break (1.7.4 and 5.3.3.2, to start with).
(see also links below).
Now, I claim that even considering this, not having provision for
_really_ empty classes is a fiasco of the standard. The so-called
"empty member c++ optimization" (see links) is not a complete cure: it
relies on the availability of a non-empty member on which empty
classes are aggregated. Such a member is not always available. In
template metaprogramming world, one would prize the ability to create
possibly empty classes made of several possibly all empty members.
Extending the language, say via a keyword, to include ``ghost''
classes (by lack of a better name), which really occupy zero storage
when they are empty and present as members of anything would be, IMHO,
a very valuable addition. Implications would include:
- distinct objects of two non-ghost types would still reside at
distinct addresses. In case one of the object is a ghost class, this
would no longer hold.
- pointer arithmetic on ghost class pointers would be illegal.
Does such a thing make sense to anybody ?
Some relevant links include:
http://www.research.att.com/~bs/bs_faq2.html#sizeof-empty
http://www.cantrip.org/emptyopt.html
http://tinyurl.com/36r26