这是合法的C ++吗?
struct foo
{
int a[100];
int b[sizeof(a) / sizeof(a[0])];
};
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
答案 0 :(得分:18)
这在C ++ 03中是非法的,因为这些成员是非静态数据库。
从C ++ 11开始,这是合法的,因为在未评估的操作数中,您可以使用非静态数据库而无需具有相应的对象。
答案 1 :(得分:1)
试试这个:这是MSVC 2010和MSVC 2012的解决方法
struct Aoo
{
typedef int ArrayType;
ArrayType a[100];
};
struct foo : public Aoo
{
enum {bSize = sizeof(Aoo) / sizeof(Aoo::ArrayType)};
int b[bSize];
};