12

我试图编译下面的代码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++我得到了预期的错误。

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

所以我发现了一个错误Clang吗?

4

2 回答 2

9

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

该规则在标准中表示:

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

于 2010-08-20T13:44:07.873 回答
4

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.

于 2010-08-22T22:04:28.373 回答