www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is there a way to get the size of a class object statically?

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
I figured out a way to get the offsetof any member statically:

class A {
     char a;
     int b;
     char c;
}

void main()
{
     int[A.init.a.offsetof]  x;
}

Unfortunately, I can't figure a way to get the class' size statically. 
This doesn't work:

     int[A.classinfo.init.length]  x;

Any way to fetch the size of A?


Andrei
Oct 04 2009
next sibling parent reply Lutger <lutger.blijdestijn gmail.com> writes:
Andrei Alexandrescu wrote:

 I figured out a way to get the offsetof any member statically:
 
 class A {
      char a;
      int b;
      char c;
 }
 
 void main()
 {
      int[A.init.a.offsetof]  x;
 }
 
 Unfortunately, I can't figure a way to get the class' size statically.
 This doesn't work:
 
      int[A.classinfo.init.length]  x;
 
 Any way to fetch the size of A?
 
 
 Andrei

This question was on .learn, from where the answer (by Jarret) was: __traits(classInstanceSize, Class)
Oct 05 2009
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Lutger wrote:
 Andrei Alexandrescu wrote:
 
 I figured out a way to get the offsetof any member statically:

 class A {
      char a;
      int b;
      char c;
 }

 void main()
 {
      int[A.init.a.offsetof]  x;
 }

 Unfortunately, I can't figure a way to get the class' size statically.
 This doesn't work:

      int[A.classinfo.init.length]  x;

 Any way to fetch the size of A?


 Andrei

This question was on .learn, from where the answer (by Jarret) was: __traits(classInstanceSize, Class)

Thanks, Lutger and Max! Andrei
Oct 05 2009
prev sibling next sibling parent Max Samukha <spambox d-coding.com> writes:
On Mon, 05 Oct 2009 01:47:21 -0500, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:

I figured out a way to get the offsetof any member statically:

class A {
     char a;
     int b;
     char c;
}

void main()
{
     int[A.init.a.offsetof]  x;
}

Unfortunately, I can't figure a way to get the class' size statically. 
This doesn't work:

     int[A.classinfo.init.length]  x;

Any way to fetch the size of A?


Andrei

__traits(classInstanceSize, A)
Oct 05 2009
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 05 Oct 2009 14:13:06 +0400, Nick Sabalausky <a a.a> wrote:

 "Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message
 news:hac4pl$1s2e$1 digitalmars.com...
 I figured out a way to get the offsetof any member statically:

 class A {
     char a;
     int b;
     char c;
 }

 void main()
 {
     int[A.init.a.offsetof]  x;
 }

 Unfortunately, I can't figure a way to get the class' size statically.
 This doesn't work:

     int[A.classinfo.init.length]  x;

 Any way to fetch the size of A?

sizeof(A) doesn't work?

sizeof(A) doesn't compile. A.sizeof == Object.sizeof == (void*).sizeof
Oct 05 2009