Size of a non-static member of a class

May 7, 2022·

1 min read

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


sizeof operator:

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

Hence, we can write:

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


c++0x allow that:

// a more elegant way

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.


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

Getting the size of member variable