Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-11-05


2478. Properties of explicit specializations of implicitly-instantiated class templates

Section:13.9.4  [temp.expl.spec]    Status:C++23    Submitter:Mark Hall    Date:2021-02-02

[Accepted as a DR at the February, 2023 meeting.]

According to 13.9.4 [temp.expl.spec] paragraph 16,

A member or a member template of a class template may beexplicitly specialized for a given implicit instantiation ofthe class template, even if the member or member template isdefined in the class template definition. An explicitspecialization of a member or member template is specifiedusing the syntax for explicit specialization.

The relationship between this construct and paragraph14 is not clear:

Whether an explicit specialization of a function or variabletemplate is inline, constexpr, or an immediate function isdetermined by the explicit specialization and is independentof those properties of the template.

(See also 9.2.6 [dcl.constexpr] paragraph 1, note1.) Is this intended to apply to explicit specializationsof members of implicitly-instantiated class templates? Forexample:

  template<typename T> struct S {    int f();    constexpr int g();  };  template<> constexpr int S<int>::f() {  // OK, constexpr?    return 0;  }  template<> int S<int>::g() {            // OK, not constexpr?    return 0;  }

There is implementation divergence on the treatment ofthis example. This divergence may relate to interpretationof the requirement in 9.2.6 [dcl.constexpr] paragraph1,

If any declaration of a function or function template has aconstexpr orconsteval specifier, thenall its declarations shall contain the same specifier.

Is an explicit specialization of a member of animplicitly-instantiated class template a declaration ofthat member? A similar question also applies to theconstinit specifier as specified in9.2.7 [dcl.constinit] paragraph 1:

If the specifier is applied to any declaration of avariable, it shall be applied to the initializingdeclaration.

(Note thatconstinit is not mentioned in13.9.4 [temp.expl.spec] paragraph 14.) For example:

  template<typename T> struct S {    static constinit T x;  };  template<> int S<int>::x = 10;    //constinit required?  extern char c;  template<> short S<char>::x = c;  // error,c not constant?

(Possibly relevant is the fact that default arguments areprohibited in explicit specializations of member functionsof implicitly-instantiated class templates, per13.9.4 [temp.expl.spec] bullet 21.3.)

CWG 2022-11-10

A specialization of a member of a class template redeclares themember of the primary template and thus the redeclaration rulesapply. In passing, it was noticed that the rule governing explicitspecializations in general omitted treatment ofconstinit,which was considered an oversight.

Proposed resolution (approved 2023-02-09):

Change in 13.9.4 [temp.expl.spec] paragraph 13 as follows:

Whether an explicit specialization of a function or variable templateis inline, constexpr,constinit, orconstevalan immediate functionis determined by the explicit specialization and is independent ofthose properties of the template.



[8]ページ先頭

©2009-2025 Movatter.jp