Thread overview
class.sizeof
Nov 14, 2008
Denis Koroskin
Nov 14, 2008
Denis Koroskin
Nov 14, 2008
Sean Kelly
Nov 14, 2008
Tim M
Nov 14, 2008
bearophile
Nov 14, 2008
Sean Kelly
November 14, 2008
I can't find a way to get a class sizeof property - it returns 4 (32bit pointer size) always. I tried many ways but still can't figure out how to do this. Documentation says that

".sizeof applied to a class object returns the size of the class reference, not the class instantiation."

Is it possible? Any idea is much appreciated.
November 14, 2008
Denis Koroskin wrote:
> I can't find a way to get a class sizeof property - it returns 4 (32bit pointer size) always. I tried many ways but still can't figure out how to do this. Documentation says that
> 
> ".sizeof applied to a class object returns the size of the class reference, not the class instantiation."
> 
> Is it possible? Any idea is much appreciated.

No nice way to do it..

auto sz = myclass.classinfo.init.length;

problem this is not a compiletime constant,

In D2 you can use __traits to get it...

Kinda lame if you ask me :P

how about .instancesizeof ? I doubt that's even a remotely common variable name ...

-Tomas
November 14, 2008
On Fri, 14 Nov 2008 21:06:39 +0300, Tomas Lindquist Olsen <tomas@famolsen.dk> wrote:

> Denis Koroskin wrote:
>> I can't find a way to get a class sizeof property - it returns 4 (32bit pointer size) always. I tried many ways but still can't figure out how to do this. Documentation says that
>>  ".sizeof applied to a class object returns the size of the class reference, not the class instantiation."
>>  Is it possible? Any idea is much appreciated.
>
> No nice way to do it..
>
> auto sz = myclass.classinfo.init.length;
>
> problem this is not a compiletime constant,
>
> In D2 you can use __traits to get it...
>
> Kinda lame if you ask me :P
>
> how about .instancesizeof ? I doubt that's even a remotely common variable name ...
>
> -Tomas

__traits trick works nice, thank you!

BTW, I found small inconsistency/bug:

template SizeOfPointee(T)
{
    static if (is (T t)) {
        enum SizeOfPointee = typeof(*t).sizeof;
    }
}

template SizeOfPointer(T)
{
    static if (is (T t)) {
        enum SizeOfPointer = typeof(t).sizeof;
    }
}

int i = SizeOfPointee!(int*); // works ok
int j = SizeOfPointer!(int*); // fails

I believe both should work.
November 14, 2008
Tomas Lindquist Olsen wrote:
> Denis Koroskin wrote:
>> I can't find a way to get a class sizeof property - it returns 4 (32bit pointer size) always. I tried many ways but still can't figure out how to do this. Documentation says that
>>
>> ".sizeof applied to a class object returns the size of the class reference, not the class instantiation."
>>
>> Is it possible? Any idea is much appreciated.
> 
> No nice way to do it..
> 
> auto sz = myclass.classinfo.init.length;
> 
> problem this is not a compiletime constant,
> 
> In D2 you can use __traits to get it...
> 
> Kinda lame if you ask me :P
> 
> how about .instancesizeof ? I doubt that's even a remotely common variable name ...

I added an "isizeof" proposal to the issue tracker maybe two years ago.  Feel free to vote it up if you'd like.


Sean
November 14, 2008
On Fri, Nov 14, 2008 at 2:35 PM, Sean Kelly <sean@invisibleduck.org> wrote:
> Tomas Lindquist Olsen wrote:
>>
>> Denis Koroskin wrote:
>>>
>>> I can't find a way to get a class sizeof property - it returns 4 (32bit pointer size) always. I tried many ways but still can't figure out how to do this. Documentation says that
>>>
>>> ".sizeof applied to a class object returns the size of the class reference, not the class instantiation."
>>>
>>> Is it possible? Any idea is much appreciated.
>>
>> No nice way to do it..
>>
>> auto sz = myclass.classinfo.init.length;
>>
>> problem this is not a compiletime constant,
>>
>> In D2 you can use __traits to get it...
>>
>> Kinda lame if you ask me :P
>>
>> how about .instancesizeof ? I doubt that's even a remotely common variable name ...
>
> I added an "isizeof" proposal to the issue tracker maybe two years ago.
>  Feel free to vote it up if you'd like.

But W will say "it's already accessible through __traits."  Not to sound fatalistic, but do you honestly think this would be added to D1?
November 14, 2008
A few years too late to by why isn't all classes objects, an instance of a special virtual class called Class.

On Sat, 15 Nov 2008 08:55:29 +1300, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote:

> On Fri, Nov 14, 2008 at 2:35 PM, Sean Kelly <sean@invisibleduck.org> wrote:
>> Tomas Lindquist Olsen wrote:
>>>
>>> Denis Koroskin wrote:
>>>>
>>>> I can't find a way to get a class sizeof property - it returns 4 (32bit
>>>> pointer size) always. I tried many ways but still can't figure out how to do
>>>> this. Documentation says that
>>>>
>>>> ".sizeof applied to a class object returns the size of the class
>>>> reference, not the class instantiation."
>>>>
>>>> Is it possible? Any idea is much appreciated.
>>>
>>> No nice way to do it..
>>>
>>> auto sz = myclass.classinfo.init.length;
>>>
>>> problem this is not a compiletime constant,
>>>
>>> In D2 you can use __traits to get it...
>>>
>>> Kinda lame if you ask me :P
>>>
>>> how about .instancesizeof ? I doubt that's even a remotely common variable
>>> name ...
>>
>> I added an "isizeof" proposal to the issue tracker maybe two years ago.
>>  Feel free to vote it up if you'd like.
>
> But W will say "it's already accessible through __traits."  Not to
> sound fatalistic, but do you honestly think this would be added to D1?

November 14, 2008
Jarrett Billingsley:
> But W will say "it's already accessible through __traits."  Not to sound fatalistic, but do you honestly think this would be added to D1?

Generally I think that having a fully frozen language (D1), that can't accept improvements, is bad.
And I think that in an open source language, if enough people what to add a feature or remove a bug, and they are able to implement/do it, then it has to be doable. Begging a single person to implement all the changes or all debugging seems silly. If enough people are willing, then such feature/debugging can be added/done to LDC, for example.

Bye,
bearophile
November 14, 2008
Jarrett Billingsley wrote:
> On Fri, Nov 14, 2008 at 2:35 PM, Sean Kelly <sean@invisibleduck.org> wrote:
>>
>> I added an "isizeof" proposal to the issue tracker maybe two years ago.
>>  Feel free to vote it up if you'd like.
> 
> But W will say "it's already accessible through __traits."  Not to
> sound fatalistic, but do you honestly think this would be added to D1?

I don't :-)  If it were, it would have been added already.


Sean