17 Templates [temp]

17.6 Template declarations [temp.decls]

17.6.1 Class templates [temp.class]

17.6.1.1 Member functions of class templates [temp.mem.func]

A member function of a class template may be defined outside of the class template definition in which it is declared.
[Example
:
template<class T> class Array {
  T* v;
  int sz;
public:
  explicit Array(int);
  T& operator[](int);
  T& elem(int i) { return v[i]; }
};
declares three function templates.
The subscript function might be defined like this:
template<class T> T& Array<T>::operator[](int i) {
  if (i<0 || sz<=i) error("Array: range error");
  return v[i];
}
A constrained member function can be defined out of line:
template<typename T> concept C = requires {
  typename T::type;
};

template<typename T> struct S {
  void f() requires C<T>;
  void g() requires C<T>;
};

template<typename T>
  void S<T>::f() requires C<T> { }      // OK
template<typename T>
  void S<T>::g() { }                    // error: no matching function in S<T>
end example
]
The template-arguments for a member function of a class template are determined by the template-arguments of the type of the object for which the member function is called.
[Example
:
The template-argument for Array<T>​::​operator[]() will be determined by the Array to which the subscripting operation is applied.
Array<int> v1(20);
Array<dcomplex> v2(30);

v1[3] = 7;                              // Array<int>​::​operator[]()
v2[3] = dcomplex(7,8);                  // Array<dcomplex>​::​operator[]()
end example
]