c++ - Why doesn't sizeof parse struct members? -
I know that sizeof
is a compile time calculation, but it seems strange to me: The compiler can either take a type of name, or an expression (on which it reduces the type). But how do you identify one type within a class? This is the only way to pass an expression, which seems very clunky.
struct x {int x; }; Int main () {// Return size (X :: X); // Return size does not work (x ::) :: x); // works, and X needs to be default-able)
An alternative method works without the need for a default constructor:
return size ((x *) 0) -> gt; x);
You can wrap it in a macro, so that it can be read better:
#define member_sizeof (T, F) sizeof (( * T *) 0) - & gt; F)
Comments
Post a Comment