当声明另一个成员时,你可以使用一个成员的sizeof? [英] Can you use the sizeof one member when declaring another member?

查看:330
本文介绍了当声明另一个成员时,你可以使用一个成员的sizeof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是合法的C ++吗?

Is this legal C++?

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

GCC 4.6接受它,但MSVC 2012不接受。这似乎应该是对我很好,但有一点谷歌没有帮助,我不知道在哪里看标准。

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给出以下输出:

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


推荐答案

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

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

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

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

这篇关于当声明另一个成员时,你可以使用一个成员的sizeof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆