views:

268

answers:

2

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?

+6  A: 

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.

PC2st
+2  A: 

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.

Faisal Vali