Josh Lee
Josh Lee

Reputation: 177604

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?

Upvotes: 5

Views: 2415

Answers (2)

aschepler
aschepler

Reputation: 72356

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.

Upvotes: 7

hazydev
hazydev

Reputation: 344

You don't need to declare twice.

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

Is all that is needed.

Upvotes: -1

Related Questions