是否允许重复初始化静态成员?

发布于 2024-09-14 21:30:12 字数 820 浏览 2 评论 0 原文

我尝试使用 Clang 编译下面的代码 使用

class Prasoon{

  static const int dummy = 0;

};
int const Prasoon::dummy = 0;

int main(){}

Clang 编译时,上面的代码没有给出任何错误。

prasoon@prasoon-desktop ~ $ clang++ --version
clang version 2.8 (trunk 107611)
Target: i386-pc-linux-gnu
Thread model: posix
prasoon@prasoon-desktop ~ $ cat bug.cpp
class Prasoon{

      private:
      static const int dummy = 0;

    };

int const Prasoon::dummy = 0;

int main(){}
prasoon@prasoon-desktop ~ $ clang++ bug.cpp
prasoon@prasoon-desktop ~ $ 

但是当我使用 g++ 编译相同的代码时code> 我收到了预期的错误。

prasoon@prasoon-desktop ~ $ g++ bug.cpp
bug.cpp:8: error: duplicate initialization of ‘Prasoon::dummy’

那么我在 Clang 中发现了错误吗?

I tried to compile the code below with Clang

class Prasoon{

  static const int dummy = 0;

};
int const Prasoon::dummy = 0;

int main(){}

The above code did not give any error when compiled with Clang.

prasoon@prasoon-desktop ~ $ clang++ --version
clang version 2.8 (trunk 107611)
Target: i386-pc-linux-gnu
Thread model: posix
prasoon@prasoon-desktop ~ $ cat bug.cpp
class Prasoon{

      private:
      static const int dummy = 0;

    };

int const Prasoon::dummy = 0;

int main(){}
prasoon@prasoon-desktop ~ $ clang++ bug.cpp
prasoon@prasoon-desktop ~ $ 

But when I compiled the same code with g++ I got an error as expected.

prasoon@prasoon-desktop ~ $ g++ bug.cpp
bug.cpp:8: error: duplicate initialization of ‘Prasoon::dummy’

So have I found a bug in Clang?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

无需解释 2024-09-21 21:30:12

是的,您发现了一个错误。

该规则在标准中表述为:

9.4.2-3:
如果静态数据成员是 const
文字类型,其声明在
类定义可以指定一个
大括号或等号初始化器,其中
每个初始化子句都是
赋值表达式是一个常量
表达。静态数据成员
文字类型可以在
使用 constexpr 定义类
说明符;如果是这样,其声明
应指定一个
大括号或等于初始化器,其中
每个初始化子句都是
赋值表达式是一个常量
表达。 [注:在这两个
在这种情况下,该成员可能会出现在
常量表达式。 — 尾注]
成员仍应定义在
命名空间范围,如果它被用在
程序和命名空间范围
定义不应包含
初始化程序。

Yes, you have found a bug.

The rule is expressed in the standard:

9.4.2-3:
If a static data member is of const
literal type, its declaration in the
class definition can specify a
brace-or- equal-initializer in which
every initializer-clause that is an
assignment-expression is a constant
expression. A static data member of
literal type can be declared in the
class definition with the constexpr
specifier; if so, its declaration
shall specify a
brace-or-equal-initializer in which
every initializer-clause that is an
assignment-expression is a constant
expression. [ Note: In both these
cases, the member may appear in
constant expressions. — end note ] The
member shall still be defined in a
namespace scope if it is used in the
program and the namespace scope
definition shall not contain an
initializer.

蓝眼睛不忧郁 2024-09-21 21:30:12

是的,这确实是一个错误。我偶然发现了您向 clang 提交的 错误报告 - 感谢您花时间提交它 :) 虽然此错误最初在 4/ 上被记录为 错误 23/10,你的提交引起了我的注意,我提交了一个简单的 补丁发送给开发者小组进行审核。

Yes this is indeed a bug. I stumbled upon your bug report to clang -- thanks for taking the time to submit it :) While this bug was initially logged as a bug on 4/23/10, your submission brought it to my attention and I have submitted a simple patch to the developer's group for their review.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文