Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use the sizeof one member when declaring another member?

Tags:

c++

Is this legal C++?

struct foo
{
  int a[100];
  int b[sizeof(a) / sizeof(a[0])];
};

GCC 4.6 accepts it, but MSVC 2012 doesn't. It seems like it should be fine to me, but a bit of Googling didn't help and I don't know where to look in the standard.

MSVC 2012 gives the following output:

error C2327: 'foo::a' : is not a type name, static, or enumerator
error C2065: 'a' : undeclared identifier
error C2070: ''unknown-type'': illegal sizeof operand
warning C4200: nonstandard extension used : zero-sized array in struct/union
like image 355
Karu Avatar asked Sep 20 '12 08:09

Karu


1 Answers

This was illegal in C++03 because these members are nonstatic datamembers.

Starting from C++11 this is legal since in an unevaluated operand you can use nonstatic datamembers without having a corresponding object.

like image 93
Johannes Schaub - litb Avatar answered Nov 09 '22 09:11

Johannes Schaub - litb