简体   繁体   English

我在Clang中发现了一个错误吗?

[英]Have I found a bug in Clang?

I tried to compile the code below with Clang 我试着用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. 使用Clang.编译时,上面的代码没有给出任何错误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. 但是当我使用g++编译相同的代码时,我得到了一个错误。

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

So have I found a bug in Clang ? 我在Clang发现了一个错误吗?

Yes, you have found a bug. 是的,你发现了一个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. 9.4.2-3:如果静态数据成员是const文字类型,则它在类定义中的声明可以指定一个大括号或等于初始化器,其中作为赋值表达式的每个initializer子句都是一个常量表达式。 A static data member of literal type can be declared in the class definition with the constexpr specifier; 可以使用constexpr说明符在类定义中声明文字类型的静态数据成员; 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. 如果是这样,它的声明应指定一个大括号或等于初始化器,其中作为赋值表达式的每个initializer子句都是一个常量表达式。 [ 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. - 结束注释] 如果在程序中使用该成员,并且命名空间作用域定义不包含初始化程序,则该成员仍应在命名空间作用域中定义。

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. 我偶然发现了你的错误报告 - 感谢花时间提交它:)虽然这个错误最初在4月23日被记录为错误 ,但是你的提交引起了我的注意,我提交了一个简单的补丁给开发者小组进行审核。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM