Size of a class instance at compile time

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sat Jan 13 13:03:22 PST 2007


Frits van Bommel wrote:
> [1]: e.g. I haven't tested using anything with explicit align(N) 
> declarations, not sure what happens there (would .alignof be adjusted?).

Yep, definitely breaks there. But I did find a much more elegant 
implementation that *does* work in that case (as well as in normal cases):

-----
template InstanceSize(T)
{
     const InstanceSize =
         T.tupleof[$-1].offsetof + T.tupleof[$-1].sizeof;
}
------

_Way_ shorter, and always provides the correct answer under two simple 
assumptions:
1) The last non-static member is also last in the layout
2) There's no padding at the end.

Assumption (1) is pretty likely to be broken if and when member 
reordering is implemented, though.


More information about the Digitalmars-d-learn mailing list