[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

"packed" objects



Proposed:

We need a way to "pack" objects over inheritance boundaries, and possibly
between labeled fields of a declaration.  I do not propose here exactly
what the "right" way to do this is.

I claim this is important because I see lots of examples of serious C++
programmers implementing "hacks" to "pack" objects across inheritence
boundaries.  Examples include:  Base class B needs two bits, and derived
class D needs three bits, so let's implement a bit sharing hack between 
base and derived, rather than being able to use bit fields, and automatically
have those fields "packed" into one word in the final structure.  Or base class
B needs a byte, and derived class B needs a byte, but each is going to 
be placed in their own word-aligned slot, so instead implement a hack so
that both bytes can reside in one word.  Features commonly use should be
part of the language, not implemented via "hack" word-arounds.

---

Possible full or partial "solutions" to this problem include:

* Throw out the current C++ restrictions on field ordering entirely.  What
does it buy us anyway?  Use 'extern "C"' where an object needs backwards
compatibility to "C" packing order.

* Require current field ordering restrictions be maintained on things declared
"struct", but removed restrictions on anything declared "class."

* Introduce an explicit keyword "packed" or similar.  Declaring a base class
"packed" turns off field ordering restrictions in that class and its 
derivatives, as well as representing a directive to the compiler that small
size is to be preferred to fast access.  Possibly vtable ptrs could also 
be replaced with smaller type tags in such classes....

....

[disclaimer: this posting respresents the opinions of an individual C++ user]