Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A type template parameter is a compile-time placeholder for a data type within a template declaration. It enables the definition of generic classes, functions, variables, and type aliases by deferring the specification of concrete types until the template is instantiated by the compiler.

Syntax and Keywords

Type template parameters are declared within angle brackets (< >) preceding the template definition. They are introduced using either the typename or class keyword. In the context of declaring a type template parameter, these two keywords are semantically identical and strictly interchangeable.
// Using 'typename'
template <typename T>
void functionName(T parameter);

// Using 'class'
template <class U>
class ClassName {
    U memberVariable;
};

// Multiple type parameters
template <typename Key, class Value>
struct KeyValuePair;

Default Type Arguments

A type template parameter can be provided a default concrete type using the = token as a default argument specifier. If a type argument is omitted during instantiation, the compiler substitutes the default type. For class, variable, and alias templates, if a parameter specifies a default type, all subsequent template parameters in the list must also specify defaults. During instantiation, default template arguments are evaluated strictly left-to-right. This left-to-right evaluation allows subsequent default arguments to depend on preceding template parameters. However, since C++11, the restriction requiring trailing defaults does not apply to function templates. Function templates can have template parameters without defaults following those with defaults, as the trailing parameters are typically resolved via template argument deduction.
#include <vector>

// Class template: subsequent parameters must have defaults.
// Evaluated left-to-right: 'U' can safely depend on 'T'.
template <typename T = int, typename U = std::vector<T>>
class DataNode {
    T primary;
    U secondary;
};

// Function template (C++11+): trailing parameters do not require defaults
template <typename ReturnType = void, typename InputType>
ReturnType process(InputType value);

Variadic Type Parameters (Parameter Packs)

A type template parameter pack accepts zero or more type arguments. It is declared by appending an ellipsis (...) immediately after the typename or class keyword. The resulting parameter pack must be expanded within the template body to be utilized.
template <typename... Types>
class Tuple;

// Instantiation resolves 'Types' to a list of distinct types
// Tuple<int, double, char>

Constrained Type Parameters (C++20)

With the introduction of C++20 Concepts, type template parameters can be constrained directly in the template parameter list. The typename or class keyword is replaced by the name of a Concept, which enforces compile-time requirements on the type argument. If the provided type does not satisfy the concept, substitution fails.
#include <concepts>

// 'T' is a type template parameter constrained by the 'std::integral' concept
template <std::integral T>
void processInteger(T value);

Scope and Shadowing Rules

The scope of a type template parameter extends from its point of declaration to the end of the template definition.
  1. A type template parameter cannot be redeclared within the same template parameter list.
  2. A type template parameter shadows identical names declared in an enclosing outer scope.
  3. The C++ standard explicitly forbids redeclaring or shadowing a template parameter within its scope. Any attempt to declare a variable, type, or function with the same name as the template parameter inside the template body or its nested scopes (such as member functions) will result in a hard compilation error.
typedef double T; // Outer scope type alias

template <typename T> // Shadows the outer 'T' (double)
struct Container {
    T data; // Refers to the template parameter, not 'double'
    
    void method() {
        // int T = 5; // ERROR: declaration of 'T' shadows template parameter
    }
};
Master C++ with Deep Grasping Methodology!Learn More