www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: .isizeof property for compile-time instance size

reply Sean Kelly <sean f4.ca> writes:
(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.
Apr 09 2006
parent reply S. Chancellor <dnewsgr mephit.kicks-ass.org> writes:
On 2006-04-09 13:28:41 -0700, Sean Kelly <sean 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.
Apr 10 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
S. Chancellor wrote:
 On 2006-04-09 13:28:41 -0700, Sean Kelly <sean 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. :)
As long as there's some way to get the size of classes at compile time I'll be happy :-) Sean
Apr 10 2006
parent reply Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Sean Kelly wrote:
 
 As long as there's some way to get the size of classes at compile time 
 I'll be happy :-)
 
 
 Sean
Why do you want this anyway? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Apr 14 2006
parent Sean Kelly <sean f4.ca> writes:
Bruno Medeiros wrote:
 Sean Kelly wrote:
 As long as there's some way to get the size of classes at compile time 
 I'll be happy :-)
Why do you want this anyway?
Mostly to be able to construct classes in places other than GC managed memory. Memory mapped files for example, fixed-size arrays within GCed classes, stack-based arrays, etc. Some of these can be accomplied using .classinfo.init.sizeof or whatever, but others require the size to be known at compile-time. Sean
Apr 14 2006
prev sibling parent reply Frank Benoit <benoit__ __tionex.de> writes:
 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. :)
If you have a template for type T and want to know the size for storing it T.sizeof should return 4 for a reference type. So it seems to me, a new name like ".isizeof" is better.
Apr 10 2006
parent S. Chancellor <dnewsgr mephit.kicks-ass.org> writes:
On 2006-04-10 11:39:48 -0700, Frank Benoit <benoit__ __tionex.de> said:

 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. :)
If you have a template for type T and want to know the size for storing it T.sizeof should return 4 for a reference type. So it seems to me, a new name like ".isizeof" is better.
Having an "ideal" reference would solve a lot more problems than just this though.
Apr 11 2006