Explicit specializations of constexpr static data members
13.9.4 [temp.expl.spec]
Mike Miller

Created on 2019-04-29.00:00:00 last changed 11 months ago


Date: 2019-05-15.00:00:00

Notes from the May, 2019 teleconference:

These examples should behave in the same way as if the class were templated: instantiate the declaration and the definition of the static data member separately. The first example should be ill-formed, because the explicit specializaation does not have an initializer.

Date: 2021-02-24.00:00:00

The status of an example like the following is not clear:

  struct S {
    template <int N> static constexpr inline int m = N;
  template <> constexpr inline int S::m<5>;

Some implementations accept this, apparently on the basis of allowing and ignoring a redeclaration of a constexpr static data member outside its class, although there is implementation divergence. Most or all implementations, however, diagnose an attempt to use such a specialization in a constant context.

Should it be required to have a definition of the explicit specialization in order to declare it outside the class in such cases?

In addition, most or all implementations accept a version of the example in which the explicit specialization contains an initializer, including allowing its use in constant contexts:

  template <> constexpr inline int S::m<5> = 2;

This would seem to be disallowed both by [class.static.data] paragraph 3,

An inline static data member may be defined in the class definition and may specify a brace-or-equal-initializer. If the member is declared with the constexpr specifier, it may be redeclared in namespace scope with no initializer (this usage is deprecated; see _N4778_.D.4 [depr.static_constexpr]).

which prohibits an initializer, and 13.9.4 [temp.expl.spec] paragraph 2,

An explicit specialization may be declared in any scope in which the corresponding primary template may be defined (_N4868_. [namespace.memdef], 11.4 [class.mem], 13.7.3 [temp.mem]).

since the definition of a constexpr static data member is inside the class.

Date User Action Args
2020-12-15 00:00:00adminsetmessages: + msg6253
2019-04-29 00:00:00admincreate