digitalmars.D.learn - Fields size property
- Andrej Mitrovic (19/19) Nov 10 2011 .sizeof on a struct works nicely since it's a POD, but this can't work
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/23) Nov 10 2011 This is the standard way:
- Andrej Mitrovic (2/5) Nov 10 2011 Thanks! It's also more reliable it seems. :)
- Dejan Lekic (18/42) Nov 18 2011 Sure it can be useful sometimes to know (roughly) the size of your class...
- Dejan Lekic (2/2) Nov 22 2011 This one is not good either because it does not include TypeInfo, etc...
- Andrej Mitrovic (2/4) Nov 22 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() { }
Nov 10 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
Nov 10 2011
On 11/11/11, Ali =C7ehreli <acehreli yahoo.com> wrote:This is the standard way: __traits(classInstanceSize, Foo) AliThanks! It's also more reliable it seems. :)
Nov 10 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.
Nov 18 2011
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
:) 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