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

后端 未结 2 1425
情歌与酒
情歌与酒 2021-01-04 01:48

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 li

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 02:22

    Try this: This is a workaround for MSVC 2010 and MSVC 2012

    struct Aoo
    {
        typedef int ArrayType;
        ArrayType a[100];
    };
    
    struct foo : public Aoo
    {   
        enum {bSize = sizeof(Aoo) / sizeof(Aoo::ArrayType)};
        int b[bSize];
    };
    

提交回复
热议问题