Namespaces
Variants
Views
Actions

Class template

From cppreference.com
< cpp‎ | language
Revision as of 12:28, 18 September 2012 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
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
explicit (C++11)
static

Special member functions
Templates
Class template
Function template
Miscellaneous
 

A class template defines a family of classes.

Contents

Syntax

template < Template:sparam > Template:sparam

Explanation

Template:sparam defines or declares a class (including struct and union), a member class or member enumeration type, a function or member function, a static data member of a class template, or a type alias. It may also define a template specialization. This page focuses on class templates.

Template:sparam is a possibly empty comma-separated list of the template parameters, each of which is either non-type parameter, a type parameter, a template parameter, or a parameter pack of any of those. This page focuses on the parameters that are not parameter packs.

Non-type template parameter

Template:sparam Template:sparam (1)
Template:sparam Template:sparam = Template:sparam (2)
Template:sparam ... Template:sparam (3) (since C++11)
1) A non-type template parameter with an optional name
2) A non-type template parameter with an optional name and a default value
3) A non-type template parameter pack with an optional name

Template:sparam is one of the following types (optionally cv-qualified, the qualifiers are ignored)

  • integral type
  • enumeration
  • pointer to object or to function
  • lvalue reference to object or to function
  • pointer to member object or to member function
  • std::nullptr_t (since C++11)

Array and function types may be written in a template declaration, but they are automatically replaced by pointer to data and pointer to function as appropriate.

When the name of a non-type template parameter is used in an expression within the body of the class template, it is an unmodifiable prvalue unless its type was an lvalue reference type.

Type template parameter

typename Template:sparam (1)
class Template:sparam (2)
typename|class Template:sparam = Template:sparam (3)
typename|class ... Template:sparam (4) (since C++11)
1) A type template parameter with an optional name
2) Exactly the same as 1)
3) A type template parameter with an optional name and a default
5) A type template parameter pack with an optional name

Template template parameter

template < Template:sparam > Template:sparam (1)
template < Template:sparam > Template:sparam = Template:sparam (2)
template < Template:sparam > ... Template:sparam (3) (since C++11)
1) A template template parameter with an optional name
2) A template template parameter with an optional name and a default
3) A template template parameter pack with an optional name

Class template instantiation

Non-type template parameters

The following limitations apply when instantiating class templates that have non-type template parameters:

  • For integral and arithmetic types, the template argument provided during instantiation must be a constant expression.
  • For pointers to objects, the template arguments have to designate the address of an object with static storage duration and a linkage (either internal or external), or a constant expression that evaluates to the appropriate null pointer value.
  • For pointers to functions, the valid arguments are pointers to functions with linkage (or constant expressions that evaluate to null pointer values).
  • For lvalue reference parameters, the argument provided at instantiation cannot be a temporary, an unnamed lvalue, or a named lvalue with no linkage.
  • For pointers to members, the argument has to be a pointer to member expressed as &Class::Member or a constant expression that evaluates to null pointer value.

In particular, this implies that string literals, addresses of array elements, and addresses of non-static members cannot be used as template arguments to instantiate templates whose corresponding non-type template parameters are pointers to data.

Example

template<typename T>
struct S {
    template<typename U> void foo(){}
};
 
template<typename T>
void bar()
{
    S<T>s;
    s.foo<T>(); // error: < parsed as less than operator
    s.template foo<T>(); // OK
}

Non-type template parameters

#include <iostream>
 
// simple non-type template parameter
template<int N>
struct S {
    int a[N];
};
 
template<const char*>
struct S2 {};
 
// complicated non-type example
template <
    char c, // integral type
    int (&ra)[5], // lvalue reference to object (of array type)
    int (*pf)(int), // pointer to function
    int (S<10>::*a)[10] // pointer to member object (of type int[10])
> struct Complicated {
    // calls the function selected at compile time
    // and stores the result in the array selected at compile time
    void foo(char base) {
        ra[4] = pf(c - base);
    }
};
 
//  S2<"fail"> s2; // Error: string literal cannot be used
char okay[] = "okay"; // static object with linkage
// S2< &okay[0] > s2; // Error: array element has no linkage
S2<okay> s2; // works
 
int a[5];
int f(int n) { return n;}
int main()
{
    S<10> s; // s.a is an array of 10 int
    s.a[9] = 4;
 
    Complicated<'2', a, f, &S<10>::a> c;
    c.foo('0');
 
    std::cout << s.a[9] << a[4] << '\n';
}

Output:

42

See also