Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 09, 2006 Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
(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. |
April 10, 2006 Re: Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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.
|
April 10, 2006 Re: Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
Posted in reply to S. Chancellor | 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
|
April 10, 2006 Re: Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
Posted in reply to S. Chancellor | > 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.
|
April 11, 2006 Re: Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit | 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.
|
April 14, 2006 Re: Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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 |
April 14, 2006 Re: Proposal: .isizeof property for compile-time instance size | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno Medeiros | 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
|
Copyright © 1999-2021 by the D Language Foundation