gpt4 book ai didi

c++ - 声明另一个成员时可以使用一个成员的大小吗?

转载 作者:IT老高 更新时间:2023-10-28 22:13:46 24 4
gpt4 key购买 nike

这是合法的 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

最佳答案

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

从 C++11 开始,这是合法的,因为在未计算的操作数中,您可以在没有相应对象的情况下使用非静态数据成员。

关于c++ - 声明另一个成员时可以使用一个成员的大小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12508771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com