www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fields size property

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
.sizeof on a struct works nicely since it's a POD, but this can't work
on classes since it just returns the pointer size.

I don't know whether this is useful all that much, but I'm curious how
large my classes are. Anyway, since I couldn't find anything in Phobos
I've got this working:

import std.traits;

auto getFieldsSizeOf(T)() {
    size_t result;
    foreach (type; RepresentationTypeTuple!Foo) {
        result += type.sizeof;
    }
    return result;
}

template fieldsSizeOf(T) {
    enum size_t fieldsSizeOf = getFieldsSizeOf!T();
}

class Foo { int x, y; }
static assert(fieldsSizeOf!Foo == 8);
void main() { }
Nov 10 2011
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/10/2011 05:44 PM, Andrej Mitrovic wrote:
 .sizeof on a struct works nicely since it's a POD, but this can't work
 on classes since it just returns the pointer size.

 I don't know whether this is useful all that much, but I'm curious how
 large my classes are. Anyway, since I couldn't find anything in Phobos
 I've got this working:

 import std.traits;

 auto getFieldsSizeOf(T)() {
      size_t result;
      foreach (type; RepresentationTypeTuple!Foo) {
          result += type.sizeof;
      }
      return result;
 }

 template fieldsSizeOf(T) {
      enum size_t fieldsSizeOf = getFieldsSizeOf!T();
 }

 class Foo { int x, y; }
 static assert(fieldsSizeOf!Foo == 8);
 void main() { }
This is the standard way: __traits(classInstanceSize, Foo) Ali
Nov 10 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 11/11/11, Ali =C7ehreli <acehreli yahoo.com> wrote:
 This is the standard way:

      __traits(classInstanceSize, Foo)

 Ali
Thanks! It's also more reliable it seems. :)
Nov 10 2011
prev sibling parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
Andrej Mitrovic wrote:

 .sizeof on a struct works nicely since it's a POD, but this can't work
 on classes since it just returns the pointer size.
 
 I don't know whether this is useful all that much, but I'm curious how
 large my classes are. Anyway, since I couldn't find anything in Phobos
 I've got this working:
 
 import std.traits;
 
 auto getFieldsSizeOf(T)() {
     size_t result;
     foreach (type; RepresentationTypeTuple!Foo) {
         result += type.sizeof;
     }
     return result;
 }
 
 template fieldsSizeOf(T) {
     enum size_t fieldsSizeOf = getFieldsSizeOf!T();
 }
 
 class Foo { int x, y; }
 static assert(fieldsSizeOf!Foo == 8);
 void main() { }
Sure it can be useful sometimes to know (roughly) the size of your classes. I modified the function to: auto getFieldsSizeOf(T)() { size_t result; foreach (type; RepresentationTypeTuple!T) { static if (is(type == class) ) { result += getFieldsSizeOf!type(); } else { result += type.sizeof; } } return result; } As you can see, it goes little bit deeper so code like this: class Bar { int x, y; Foo f; } writeln(fieldsSizeOf!Bar); gives us 16 as output not 8.
Nov 18 2011
parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
This one is not good either because it does not include TypeInfo, etc...

__traits(classInstanceSize, T)) is better choice, as Ali said... :)
Nov 22 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
:)

On 11/22/11, Dejan Lekic <dejan.lekic gmail.com> wrote:
 This one is not good either because it does not include TypeInfo, etc...

 __traits(classInstanceSize, T)) is better choice, as Ali said... :)
Nov 22 2011