constexpr: definition and declaration for constexpr members

五迷三道 提交于 2019-11-29 18:08:19

The rules governing static constexpr members changes in C++1z, which is kinda annoying.

Pre C++1z

From [class.static.data]

[...] 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. [...] The member shall still be defined in a namespace scope if it is odr-used ([basic.def.odr]) in the program and the namespace scope definition shall not contain an initializer.

Which means you are required to provide a initializer in the declaration, but also to provide a definition at namespace scope without an initializer.

Your code does exactly that.

Post C++1z

From the same paragraph [class.static.data]

[...] If the member is declared with the constexpr specifier, it may be redeclared in namespace scope with no initializer (this usage is deprecated; see [depr.static_constexpr]). [...]

And from [depr.static_constexpr]

For compatibility with prior C++ International Standards, a constexpr static data member may be redundantly redeclared outside the class with no initializer. This usage is deprecated.

So, this is still legal, but deprecated.

It should be noted static but not constexpr members (including static const members) are still required to have a definition in namespace scope if it is odr-used.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!