Size of a class instance at compile time
Daniel Keep
daniel.keep+lists at gmail.com
Sat Jan 13 23:23:46 PST 2007
Frits van Bommel wrote:
> 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.
Oh that's beautiful; I can't believe I forgot about offsetof! Thanks
very much for that: this should make the allocators a bit more efficient :)
-- Daniel
More information about the Digitalmars-d-learn
mailing list