2013-06-12 10 views
5

'constexprの' は異なるが、GCCは、それを拒否:エラー:再宣言は、クランは、このコードを受け入れる

class Foo { 
public: 
    static constexpr double kVal = 0.25f; 
}; 

const double Foo::kVal; 

解釈がある

~$ clang++ foo.cc -std=c++11 -c 
[ok] 
~$ g++ foo.cc -std=c++0x -c 
foo.cc:6:19: error: redeclaration ‘Foo::kVal’ differs in ‘constexpr’ 
foo.cc:3:34: error: from previous declaration ‘Foo::kVal’ 
foo.cc:6:19: error: declaration of ‘constexpr const double Foo::kVal’ outside of class is not definition [-fpermissive] 

(打ち鳴らす3.0とg ++ 4.6.3を使用)正しい?

答えて

5

clangが正しいです。これは、gccのチームミスリードに誰かのように見えるかmisimplemented:

7.1.5/1:

If any declaration of a function or function template has constexpr specifier, then all its declarations shall contain the constexpr specifier.

Foo::kValは明らかに関数または関数テンプレートではありません。 1つの宣言から次の宣言まで一貫性を持たせるために、constexprの使用を必要とする規格には何も表示されません。

+0

関数宣言の場合、clangとgccの両方がエラーを返します。 –

+0

@ JoshLee:それは関数にとって必須なので、ルールは変数には適用されないからです。しかし、私は個人的にそれらの間の一貫性を好むでしょう。 –

-1

2度宣言する必要はありません。

class Foo { 
    public: 
    static constexpr double kVal = 0.25f; 
}; 

すべてです。

+0

左辺値として 'kVal'を使う必要があります。 – aschepler

関連する問題