2.6.7 Size of Members

Computing the size of a struct member in the old days of C++ required to use an instance of the struct, like in:

struct foo 
{ 
  int m; 
}; 
 
int main() 
{ 
  // There is no way to apply sizeof directly to m, so we "create" 
  // an instance just to get the member. Fortunately the expression 
  // on which sizeof is applied is not evaluated. 
  printf("%ld\n", sizeof(foo().m)); 
 
  return 0; 
}

This is doable for simple structs but clearly becomes difficult if the constructor needs arguments.

In C++11 there is now a syntax to get the size of a member:

int main() 
{ 
  printf("%ld\n", sizeof(foo::m)); 
  return 0; 
}