Proposal: .isizeof property for compile-time instance size
S. Chancellor
dnewsgr at mephit.kicks-ass.org
Mon Apr 10 00:46:30 PDT 2006
On 2006-04-09 13:28:41 -0700, Sean Kelly <sean at f4.ca> said:
> (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.
I'd rather see my proposal implemented, and then classes be in terms of
true references and then ad a "psizeof" to get the size of the physical
pointer or stack variable. Of course, I would be bias toward my
proposal. :)
-S.
More information about the Digitalmars-d
mailing list