static const的地址不是const表达式吗?

发布于 2024-10-30 05:51:21 字数 159 浏览 1 评论 0原文

我虽然 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥抱我好吗 2024-11-06 05:51:21

变量的地址(无论是静态还是非静态)不是编译时常量。 enum 需要编译时常量。这就是为什么会出现错误。

事实上,GCC给出了非常明确的错误信息:

prog.cpp:7: 错误:'X::x' 不能出现在常量表达式中
prog.cpp:7: 错误: `&'不能出现在常量表达式中

请参见自己: http://ideone.com/FJk3C


但是,以下是< a href="http://ideone.com/CX0fi" rel="nofollow">允许:

class X {
    static const int x;
    enum { y = sizeof(x) }; //okay. sizeof(x) can be known at compile time!
};

不要将编译时常量与运行时常量混淆。它们是两个不同的东西。

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:

prog.cpp:7: error: ‘X::x’ cannot appear in a constant-expression
prog.cpp:7: error: `&' cannot appear in a constant-expression

See yourself : http://ideone.com/FJk3C


However, the following is allowed:

class X {
    static const int x;
    enum { y = sizeof(x) }; //okay. sizeof(x) can be known at compile time!
};

Don't confuse compile-time constants with runtime constants. They're two different things.

愿与i 2024-11-06 05:51:21

阅读 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.

绅士风度i 2024-11-06 05:51:21

它是一个常量表达式,但无法在编译时确定。地址的实际值将取决于可执行文件最终被运行该东西的任何操作系统加载程序加载到的内存区域。枚举成员需要具有可由编译器确定的值。

干杯,
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.

撩动你心 2024-11-06 05:51:21

静态对象的地址是一个常量表达式,但是
它不是一个整型常量表达式,因为它不
有一个整数类型。并将其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_casting it to an
integral type still doesn't make it an integral constant
expression, since reinterpret_cast isn't allowed in integral
constant expressions. And the initializer for an enum value
requires 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 with
an expression that doesn't have an integral type, and doesn't
have an implicit conversion to an integral type.

罪#恶を代价 2024-11-06 05:51:21

该程序格式错误,因为:

  • 对象的地址不是
    整型常量表达式
  • 取 x 的地址的 需要 a(n
    类外)
  • 在任何地方使用静态常量整型成员定义,除了整型
    常量表达式是必需的,需要定义。

The program is ill-formed because:

  • the address of an object is not a
    integral constant expression
  • taking the address of x requires a(n
    out of class) definition
  • using a static const integral member anywhere except where an integral
    constant-expression is required requires a definition.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文