班级成员范围
在下面的例子中,数组v
的大小保证是2还是3?
static const int i = 3;
class X {
char v[i];
static const int i = 2;
};
从标准来看,
3.3.6/2 类 S 中使用的名称 N 应在其上下文中以及在 S 的完整范围内重新评估时引用相同的声明
我认为这意味着“i”应为 2
重新评估的真正含义是什么?
In the following example, will the size of array v
guaranteed to be 2 or 3?
static const int i = 3;
class X {
char v[i];
static const int i = 2;
};
From the standard,
3.3.6/2 A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S
I think this means 'i' shall be 2
and what does the re-evaluation thing really means here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的行为是它应该导致错误,因为重新评估会改变含义:
第 3.3.6 节中的示例:
该示例与您的示例类似(使用
enum
而不是static const int
):当时
遇到 v[i]
时,编译器只知道enum { i = 1 };
(或static const int i = 3;
),但是当完整的类声明已知,char v[i]
会有所不同,因为i
将被重新评估为2
。The correct behavior is that it should cause an error because re-evaluation would change the meaning:
Example from section 3.3.6:
The example is similar to yours (using
enum
instead of astatic const int
):At the time
v[i]
is encountered, the compiler only knows aboutenum { i = 1 };
(orstatic const int i = 3;
, but when the full class declaration is known,char v[i]
would be different becausei
would be re-evaluated to2
.在这种情况下,数组大小应为 3。如果您逐行查看代码。编译器在构造数组时对 X::i 一无所知。如果当数组大小变为 2 时更改类内的行,那么我将首先隐藏。
Array size should be 3 in this case. If you look in your code line by line. Compliler know nothing about X::i when construct array. If you change lines inside class when size of array become 2 and second i will hide first.