Size of a non-static member of a class

·

1 min read

How to get the size of a non-static member of a class without construct that class?

C

sizeof operator:

When applied to an expression, sizeof does not evaluate the expression...

Hence, we can write:

sizeof(((Foo*) 0)->m);

C++

c++0x allow that:

// a more elegant way
sizeof(Foo::m);

expr.prim.id.qual:

A nested-name-specifier that denotes a class, optionally followed by the keyword template ([temp.names]), and then followed by the name of a member of either that class ([class.mem]) or one of its base classes, is a qualified-id; ... The result is an lvalue if the member is a static member function or a data member and a prvalue otherwise.

Reference

Extending sizeof to apply to non-static data members without an object (revision 1)

Getting the size of member variable