class.sizeof
Denis Koroskin
2korden at gmail.com
Fri Nov 14 10:21:46 PST 2008
On Fri, 14 Nov 2008 21:06:39 +0300, Tomas Lindquist Olsen
<tomas at famolsen.dk> wrote:
> Denis Koroskin wrote:
>> I can't find a way to get a class sizeof property - it returns 4 (32bit
>> pointer size) always. I tried many ways but still can't figure out how
>> to do this. Documentation says that
>> ".sizeof applied to a class object returns the size of the class
>> reference, not the class instantiation."
>> Is it possible? Any idea is much appreciated.
>
> No nice way to do it..
>
> auto sz = myclass.classinfo.init.length;
>
> problem this is not a compiletime constant,
>
> In D2 you can use __traits to get it...
>
> Kinda lame if you ask me :P
>
> how about .instancesizeof ? I doubt that's even a remotely common
> variable name ...
>
> -Tomas
__traits trick works nice, thank you!
BTW, I found small inconsistency/bug:
template SizeOfPointee(T)
{
static if (is (T t)) {
enum SizeOfPointee = typeof(*t).sizeof;
}
}
template SizeOfPointer(T)
{
static if (is (T t)) {
enum SizeOfPointer = typeof(t).sizeof;
}
}
int i = SizeOfPointee!(int*); // works ok
int j = SizeOfPointer!(int*); // fails
I believe both should work.
More information about the Digitalmars-d
mailing list