Thread overview
Fields size property
Nov 11, 2011
Andrej Mitrovic
Nov 11, 2011
Ali Çehreli
Nov 11, 2011
Andrej Mitrovic
Nov 18, 2011
Dejan Lekic
Nov 22, 2011
Dejan Lekic
Nov 22, 2011
Andrej Mitrovic
November 11, 2011
.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() { }
November 11, 2011
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
November 11, 2011
On 11/11/11, Ali Çehreli <acehreli@yahoo.com> wrote:
> This is the standard way:
>
>      __traits(classInstanceSize, Foo)
>
> Ali
>

Thanks! It's also more reliable it seems. :)
November 18, 2011
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.

November 22, 2011
This one is not good either because it does not include TypeInfo, etc...

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

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... :)
>