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

前端 未结 1 1017
青春惊慌失措
青春惊慌失措 2021-01-06 06:13

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 i

1条回答
  •  孤城傲影
    2021-01-06 06:40

    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.

    0 讨论(0)
提交回复
热议问题