www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - class.sizeof

reply "Denis Koroskin" <2korden gmail.com> writes:
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.
Nov 14 2008
next sibling parent reply Tomas Lindquist Olsen <tomas famolsen.dk> writes:
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
Nov 14 2008
parent reply Sean Kelly <sean invisibleduck.org> writes:
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
Nov 14 2008
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Nov 14 2008
prev sibling parent Sean Kelly <sean invisibleduck.org> writes:
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
Nov 14 2008
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
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.
Nov 14 2008
prev sibling next sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
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?
Nov 14 2008
prev sibling parent "Tim M" <a b.com> writes:
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?

Nov 14 2008