Proposal: .isizeof property for compile-time instance size
Sean Kelly
sean at f4.ca
Sun Apr 9 13:28:41 PDT 2006
(this was originally posted to D bugs, but turned out to be a mistake so
I'm reposting it here for discussion)
As D lacks a lexical specifier for class reference types there is
currently no way to determine the size of a class instance at
compile-time. A simple solution would be to add a new property, valid
for pointer and reference types, that evaluates to the size of the
referenced type. Thus:
(byte*).isizeof == byte.sizeof == 1
(byte**).isizeof == (byte*).sizeof == 4 (on 32-bit systems)
byte.isizeof == Error: property isizeof is only valid for reference types.
MyClass { int x, y; }
MyClass.isizeof == 8
(MyClass*).isizeof == MyClass.sizeof == 4 (on 32-bit systems)
Multiple levels of indirection can be eliminated using recursive
templates, so there is no reason to collapse multiple levels of
indirection and return the size of the final referenced type.
More information about the Digitalmars-d
mailing list