Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we still need to separately define static members, even if they are initialised inside the class definition?

In C++03, we had the ability to initialize const static class data members inline within the class definition, but still had to define the member if it were to be odr-used.

Is this still the case in C++11?

struct Foo
{
   static const int x = 3;
};

const int Foo::x;
// ^ required?
like image 389
Lightness Races in Orbit Avatar asked Feb 17 '23 17:02

Lightness Races in Orbit


1 Answers

Yes.

[C++11: 9.4.2/3]: If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.19). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. —end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.

This is comparable to the wording in C++03:

[C++03: 9.4.2/2]: If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

As you can see, the rule itself hasn't changed at all, beyond the introduction of rules for constexpr.

like image 164
Lightness Races in Orbit Avatar answered May 07 '23 10:05

Lightness Races in Orbit