Member templates

< cpp‎ | language
Revision as of 08:14, 30 June 2013 by Cubbi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Alternative representations
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Attributes (C++11)
typedef declaration
Type alias declaration (C++11)
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Class-specific function properties
Special member functions

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) { /* ... */ }

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 {

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)

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*
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.