Reputation: 177604
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
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 theconstexpr
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
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