1
votes

Consider the simple following:

class Test {
public:
    static constexpr int a = 1;
}

This is not considered a definition and hence needs the below definition outside of the class.

constexpr int Test::a;

Why?

1
This is not required again since C++17. - songyuanyao
what was the reason for C++11 behaves like that? I am learning c++ but finding that c++ is pretty messy!! - yapkm01
Before inline variables, all static member variables had to be defined in a single TU, to avoid breaking ODR. C++17 introduced inline variables, which makes this problem go away. - NathanOliver

1 Answers

0
votes

Why a in-class initialization of a static constexpr not a definition?

Because of One Definition Rule (ODR). The rule says that there must be exactly one definition of each non-inline non-member and static member variable. Class definitions, due to their nature, are typically included into multiple translation units. If class definition contained a variable definition, then inclusion into multiple translation units would violate the ODR.

Since C++17, the language has inline variables, so you can define such inline variables within class definitions.