Fields size property

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Nov 10 17:44:49 PST 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() { }


More information about the Digitalmars-d-learn mailing list