3.8 KiB
[temp.class.general]
13 Templates [temp]
13.7 Template declarations [temp.decls]
13.7.2 Class templates [temp.class]
13.7.2.1 General [temp.class.general]
Aclass template defines the layout and operations for an unbounded set of related types.
[Example 1:
It is possible for a single class templateList to provide an unbounded set of class definitions: one class List for every type T, each describing a linked list of elements of type T.
Similarly, a class template Array describing a contiguous, dynamic array can be defined like this:template class Array { T* v; int sz;public:explicit Array(int); T& operator; T& elem(int i) { return v[i]; }};
The prefix template specifies that a template is being declared and that atype-name T can be used in the declaration.
In other words,Array is a parameterized type withT as its parameter.
â end example]
[Note 1:
When a member of a class template is defined outside of the class template definition, the member definition is defined as a template definition with thetemplate-head equivalent to that of the class template.
The names of the template parameters used in the definition of the member can differ from the template parameter names used in the class template definition.
The class template name in the member definition is followed by the template argument list of the template-head ([temp.arg]).
[Example 2: template<class T1, class T2> struct A {void f1(); void f2();};
template<class T2, class T1> void A<T2,T1>::f1() { } // OKtemplate<class T2, class T1> void A<T1,T2>::f2() { } // error
template<class ... Types> struct B {void f3(); void f4();};
template<class ... Types> void B<Types ...>::f3() { } // OKtemplate<class ... Types> void B::f4() { } // error
template concept C = true;template concept D = true;
template struct S {void f(); void g(); void h(); template struct Inner;};
template void S::f() { } // OK, template-heads matchtemplate void S::g() { } // error: no matching declaration for Stemplate requires C // ill-formed, no diagnostic required: template-heads arevoid S::h() { } // functionally equivalent but not equivalenttemplate templatestruct S::Inner { }; // OK â end example]
â end note]
In a partial specialization, explicit specialization or explicit instantiation of a class template, the class-key shall agree in kind with the original class template declaration ([dcl.type.elab]).