static const的地址不是const表达式吗?
我虽然 address-of-static 是一个常量表达式,如下例所示,但我收到编译器错误(或者这是 C++0x 的新功能?)
class X {
static const int x;
enum { y = &x };
};
I though address-of-static was a constant expression as in the example below but I get a compiler error (or is this new to C++0x?)
class X {
static const int x;
enum { y = &x };
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
变量的地址(无论是静态还是非静态)不是编译时常量。
enum
需要编译时常量。这就是为什么会出现错误。事实上,GCC给出了非常明确的错误信息:
请参见自己: http://ideone.com/FJk3C
但是,以下是< a href="http://ideone.com/CX0fi" rel="nofollow">允许:
不要将编译时常量与运行时常量混淆。它们是两个不同的东西。
Address of a variable (be it static or non-static) is not a compile-time constant.
enum
requires compile-time constant. That is why that is giving error.In fact, GCC gives very clear error message:
See yourself : http://ideone.com/FJk3C
However, the following is allowed:
Don't confuse compile-time constants with runtime constants. They're two different things.
阅读 1998 年标准 5.19(1):“在一些地方,C++ 需要计算结果为整型或枚举常量的表达式...作为枚举器初始值设定项 (7.2)...”
此外,“整型常量表达式可以涉及仅......特别是,除了
sizeof
表达式之外,不得使用函数、类对象、指针或引用......”浮动文字被显式列为可转换为整型或枚举类型,除此之外什么也没有。
从第一个标准来看,甚至通过转换地址常量表达式来生成枚举器初始值设定项也是无效的。
Reading the 1998 standard, 5.19(1): "In several places, C++ requires expressions that evaluate to an integral or enumeration constant...as enumerator initializers (7.2)...."
Further, "An integral constant-expression can involve only....In particular, except in
sizeof
expressions, functions, class objects, pointers, or references shall not be used...."Floating literals are explicitly listed as castable to integral or enumeration type, and nothing else is.
Casting even an address constant expression to make an enumerator initializer was invalid from the first standard.
它是一个常量表达式,但无法在编译时确定。地址的实际值将取决于可执行文件最终被运行该东西的任何操作系统加载程序加载到的内存区域。枚举成员需要具有可由编译器确定的值。
干杯,
J。
It's a constant expression, but it can't be determined at compile time. The actual value of the address will depend on the region of memory that the executable ends up being loaded to by whatever OS loader is running the thing. Enum members need to have values that can be determined by the compiler.
Cheers,
J.
静态对象的地址是一个常量表达式,但是
它不是一个整型常量表达式,因为它不
有一个整数类型。并将其
reinterpret_cast
转换为整型仍然不能使其成为整型常量
表达式,因为积分中不允许使用
reinterpret_cast
常量表达式。以及
enum
值的初始值设定项需要一个整型常量表达式。
当然,就目前情况而言,您获得编译器的原因
错误是您尝试使用以下命令初始化
enum
值没有整型的表达式,并且不
隐式转换为整型。
The address of a static object is a constant expression, but
it's not an integral constant expression, because it doesn't
have an integral type. And
reinterpret_cast
ing it to anintegral type still doesn't make it an integral constant
expression, since
reinterpret_cast
isn't allowed in integralconstant expressions. And the initializer for an
enum
valuerequires an integral constant expression.
As it stands, of course, the reason you're getting a compiler
error is that you're trying to initialize an
enum
value withan expression that doesn't have an integral type, and doesn't
have an implicit conversion to an integral type.
该程序格式错误,因为:
整型常量表达式
类外)
常量表达式是必需的,需要定义。
The program is ill-formed because:
integral constant expression
out of class) definition
constant-expression is required requires a definition.