在宣布其他成员时,您可以使用一个成员的大小吗?

Kar*_*aru 17 c++

这是合法的C++吗?

struct foo
{
  int a[100];
  int b[sizeof(a) / sizeof(a[0])];
};
Run Code Online (Sandbox Code Playgroud)

GCC 4.6接受它,但MSVC 2012没有.看起来它对我来说应该没问题,但谷歌搜索没有帮助,我不知道在标准中的哪个位置.

MSVC 2012提供以下输出:

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
Run Code Online (Sandbox Code Playgroud)

Joh*_*itb 18

这在C++ 03中是非法的,因为这些成员是非静态数据库.

从C++ 11开始,这是合法的,因为在未评估的操作数中,您可以使用非静态数据库而无需具有相应的对象.