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

# C++ Type Template Parameter

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.

```cpp theme={"dark"}
// 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.

```cpp theme={"dark"}
#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.

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
#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.

```cpp theme={"dark"}
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
    }
};
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
