Member templates

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Template declarations (both class and function) can appear inside a member specification of any class, stuct, or union that aren't local class

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

template<typename T>
struct string {
    // member template function
    template<typename T2>
    int compare(const T2&);
    // constructors can be templates too
    template<typename T2>
    string(const std::string<T2>& s) { /*...*/ }
};
// out of class definition of string<T1>::compare<T2> 
template<typename T1> // for the enclosing class template
template<typename T2> // for the member template
int string<T1>::compare(const T2& s) { /* ... */ }

[edit] Member function templates

Destructor and copy constructor cannot be templates, but other special member functions can be. 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 {
    virtual void f(int);
};
struct Derived : Base {
    // this member template does not override B::f
    template <class T> void f(T);
 
    // non-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), 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
};
 
int main()
{
    A<char> ac;
    ac.f('c'); // calls template function A<char>::f<char>(int)
    ac.f(1);   // calls non-template function A<char>::f(int)
    ac.f<>(1); // calls template function A<char>::f<int>(inf)
}

[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 definition
template<typename T>
A::operator T*() {return nullptr;}
 
// explicit specialization for char*
template<>
A::operator char*() {return nullptr;}
 
// explicit instantiation
template A::operator void*();
 
int main() {
    A a;
    int* ip = a.operator int*(); // explicit call to A::operator int*()
}

During overload resolution, specialization 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 is used as if found by name lookup.

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