Movatterモバイル変換


[0]ホーム

URL:


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

2025-12-20


2307. Unclear definition of “equivalent to a nontype template parameter”

Section:13.8.3.2  [temp.dep.type]    Status:CD5    Submitter:Richard Smith    Date:2016-07-21

[Accepted as a DR at the November, 2017 meeting.]

The description of whether a template argument is equivalent to atemplate parameter in 13.8.3.2 [temp.dep.type] paragraph 3 isunclear as it applies to non-type template parameters:

A template argument that is equivalent to a template parameter (i.e., hasthe same constant value or the same type as the template parameter) can beused in place of that template parameter in a reference to the currentinstantiation. In the case of a non-type template argument, the argumentmust have been given the value of the template parameter and not anexpression in which the template parameter appears as a subexpression.

For example:

  template<int N> struct A {    typedef int T[N];    static const int AlsoN = N;    A<AlsoN>::T s; // #0, clearly supposed to be OK    static const char K = N;    A<K>::T t;     // #1, OK?    static const long L = N;    A<L>::T u;     // #2, OK?    A<(N)>::T v;   // #3, OK?    static const int M = (N);    A<M>::T w;     // #4, OK?  };

#1 narrows the template argument. This obviously should not be theinjected-class-name, becauseA<257>::T may wellbeint[1] notint[257] . However, the wording aboveseems to treat it as the injected-class-name.

#2 is questionable: there is potentially a narrowing conversion here, butit doesn't actually narrow any values for the original template parameter.

#3 is hard to decipher. On the one hand, this is an expression involvingthe template parameter. On the other hand, a parenthesized expression isspecified as being equivalent to its contained expression.

#4 should presumably go the same way that #3 does.

Proposed resolution (August, 2017):

Change 13.8.3.2 [temp.dep.type] paragraph 3 as follows:

A template argument that is equivalent to a templateparameter(i.e., has the same constant value or the same type as thetemplate parameter) can be used in place of that template parameterin a reference to the current instantiation.For a templatetype-parameter, a template argument is equivalent to a templateparameter if it denotes the same type. For a non-type template parameter,a template argument is equivalent to a template parameter if it is anidentifier that names a variable that is equivalent to thetemplate parameter. A variable is equivalent to a template parameterif

[Note: Using a parenthesized variable name breaks theequivalence. —end note]In the case of a non-typetemplate argument, the argument must have been given the value of thetemplate parameter and not an expression in which the template parameterappears as a subexpression. [Example:

  template <class T> class A {    A* p1;                   //A is the current instantiation    A<T>* p2;                //A<T> is the current instantiation    A<T*> p3;                //A<T*> is not the current instantiation    ::A<T>* p4;              //::A<T> is the current instantiation    class B {      B* p1;                 //B is the current instantiation      A<T>::B* p2;           //A<T>::B is the current instantiation      typename A<T*>::B* p3; //A<T*>::B is not the current instantiation    };  };  template <class T> class A<T*> {    A<T*>* p1;               //A<T*> is the current instantiation    A<T>* p2;                //A<T> is not the current instantiation  };  template <class T1, class T2, int I> struct B {    B<T1, T2, I>* b1;        // refers to the current instantiation    B<T2, T1, I>* b2;        // not the current instantiation    typedef T1 my_T1;    static const int my_I = I;    static const int my_I2 = I+0;    static const int my_I3 = my_I;    static const long my_I4 = I;    static const int my_I5 = (I);    B<my_T1, T2, my_I>* b3;   // refers to the current instantiation    B<my_T1, T2, my_I2>* b4;  // not the current instantiation    B<my_T1, T2, my_I3>* b5;  // refers to the current instantiation    B<my_T1, T2, my_I4>* b6;  // not the current instantiation    B<my_T1, T2, my_I5>* b7;  // not the current instantiation  };

end example]

Additional note (November, 2017):

It was observed that the proposed resolution does not address partialspecializations, which also depend on the definition of equivalence. For example:

  template <typename T, unsigned N> struct A;  template <typename T> struct A<T, 42u> {    typedef int Ty;    static const unsigned num = 42u;    static_assert(!A<T, num>::Ty(), "");  };  A<int, 42u> a; // GCC, MSVC, ICC accepts; Clang rejects

The issue is being returned to "review" status in order to considerthese additional questions.

Notes from the November, 2017 (Albuquerque) meeting:

CWG decided to proceed with this resolution and deal withpartial specialization in a separate issue.




[8]ページ先頭

©2009-2026 Movatter.jp