That's because in C++17, constexpr
implies inline
, allowing in-line definitions with initialisers. A more fair comparison would be with static inline int data{};
, which looks the same.
A constexpr
variable has to have an initialiser (presumably so it can be used in constant expressions). Before C++17, this means that you have to write:
class Test
{
private:
static int data;
public:
static constexpr int MAX{ 10 };
public:
int getData() const { return data; }
void setData(int rhs) { data = rhs; }
};
constexpr int Test::MAX;
int Test::data{};
to be able to ODR-use MAX
(essentially "take the address of"/"bind a reference to") since inline
static data members didn't exist. This is also similar for constexpr
and non-constexpr
static data members.