简体   繁体   中英

Have I found a bug in 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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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