Namespaces
Variants
Actions

Member templates

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications(until C++17*)
noexcept specifier(C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr(C++11)
consteval(C++20)
constinit(C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr(C++11)
User-defined(C++11)
Utilities
Attributes(C++11)
Types
typedef declaration
Type alias declaration(C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 
 
 
 

Template declarations (class, function, and variables(since C++14)) can appear inside a member specification of any class, struct, or union that are not local classes.

#include <algorithm>#include <iostream>#include <string>#include <vector>   struct Printer {// generic functorstd::ostream& os; Printer(std::ostream& os): os(os){}template<typename T>void operator()(const T& obj){ os << obj <<' ';}// member template};   int main(){std::vector<int> v{1,2,3};std::for_each(v.begin(), v.end(), Printer(std::cout));std::string s{"abc"}; std::ranges::for_each(s, Printer(std::cout));}

Output:

1 2 3 a b c

Partial specializations of member template may appear both at class scope and at enclosing namespace scope. Explicit specializations may appear in any scope in which the primary template may appear.

struct A {template<class T>struct B;// primary member templatetemplate<class T>struct B<T*>{};// OK: partial specialization// template<> struct B<int*> {}; // OK via CWG 727: full specialization};template<>struct A::B<int*>{};// OKtemplate<class T>struct A::B<T&>{};// OK

If the enclosing class declaration is, in turn, a class template, when a member template is defined outside of the class body, it takes two sets of template parameters: one for the enclosing class, and another one for itself:

template<typename T1>struct string {// member template functiontemplate<typename T2>int compare(const T2&);// constructors can be templates tootemplate<typename T2> string(conststd::basic_string<T2>& s){/*...*/}};// out of class definition of string<T1>::compare<T2> template<typename T1>// for the enclosing class templatetemplate<typename T2>// for the member templateint string<T1>::compare(const T2& s){/* ... */}

Contents

[edit]Member function templates

Destructors and copy constructors cannot be templates. If a template constructor is declared which could be instantiated with the type signature of a copy constructor, the implicitly-declared copy constructor is used instead.

A member function template cannot be virtual, and a member function template in a derived class cannot override a virtual member function from the base class.

class Base {virtualvoid f(int);};   struct Derived : Base {// this member template does not override Base::ftemplate<class T>void f(T);   // non-template member override can call the template:void f(int i) override { f<>(i);}};

A non-template member function and a template member function with the same name may be declared. In case of conflict (when some template specialization matches the non-template function signature exactly), the use of that name and type refers to the non-template member unless an explicit template argument list is supplied.

template<typename T>struct A {void f(int);// non-template member   template<typename T2>void f(T2);// member template};   // template member definitiontemplate<typename T>template<typename T2>void A<T>::f(T2){// some code}   int main(){ A<char> ac; ac.f('c');// calls template function A<char>::f<char>(char) ac.f(1);// calls non-template function A<char>::f(int) ac.f<>(1);// calls template function A<char>::f<int>(int)}


An out-of-class definition of a member function template must be equivalent to the declaration inside the class (see function template overloading for the definition of equivalency), otherwise it is considered to be an overload.

struct X {template<class T> T good(T n);template<class T> T bad(T n);};   template<class T>struct identity {using type = T;};   // OK: equivalent declarationtemplate<class V> V X::good(V n){return n;}   // Error: not equivalent to any of the declarations inside Xtemplate<class T> T X::bad(typename identity<T>::type n){return n;}

[edit]Conversion function templates

A user-defined conversion function can be a template.

struct A {template<typename T> operator T*();// conversion to pointer to any type};   // out-of-class definitiontemplate<typename T> A::operator T*(){return nullptr;}   // explicit specialization for char*template<> A::operatorchar*(){return nullptr;}   // explicit instantiationtemplate A::operatorvoid*();   int main(){ A a;int* ip = a.operatorint*();// explicit call to A::operator int*()}

During overload resolution, specializations of conversion function templates are not found by name lookup. Instead, all visible conversion function templates are considered, and every specialization produced by template argument deduction (which has special rules for conversion function templates) is used as if found by name lookup.

Using-declarations in derived classes cannot refer to specializations of template conversion functions from base classes.

A user-defined conversion function template cannot have a deduced return type:

struct S { operator auto()const{return10;}// OKtemplate<class T> operator auto()const{return42;}// error};
(since C++14)

Member variable templates

A variable template declaration may appear at class scope, in which case it declares a static data member template. See variable templates for details.

(since C++14)

[edit]Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1878C++14 operator auto was technically allowed operator auto forbidden
close