| Thread overview | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
November 14, 2008 class.sizeof | ||||
|---|---|---|---|---|
| ||||
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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | 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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tomas Lindquist Olsen | 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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tomas Lindquist Olsen | 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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: class.sizeof | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply