Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: redeclaration differs in ‘constexpr’

Clang accepts this code, but GCC rejects it:

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

const double Foo::kVal;

(Using clang 3.0 and g++ 4.6.3)

~$ 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]

Which interpretation is correct?

like image 532
Josh Lee Avatar asked Jun 12 '13 19:06

Josh Lee


1 Answers

clang is correct. It looks like somebody on the gcc team misread or 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 is obviously not a function or function template. I don't see anything else in the Standard requiring use of constexpr to be consistent from one declaration to the next.

like image 110
aschepler Avatar answered Nov 12 '22 20:11

aschepler