> ## 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++ Variable Template

A variable template (introduced in C++14) is a declaration that defines a family of variables or static data members parameterized by one or more template arguments. It extends the C++ template mechanism—previously restricted to classes, functions, and type aliases—directly to variable declarations, allowing the compiler to generate distinct, type-safe variables from a single parameterized definition.

## Syntax

The base syntax for declaring a variable template at namespace or class scope requires the `template` keyword followed by the template parameter list, the variable type, its identifier, and an optional initializer.

```cpp theme={"dark"}
template <template-parameter-list>
type variable_name = initializer;
```

## Instantiation and Memory Semantics

When a variable template is instantiated with a specific set of template arguments, the compiler generates a unique variable. Each instantiation occupies its own distinct memory address.

```cpp theme={"dark"}
template <typename T>
constexpr T pi = T(3.1415926535897932385L);

// Implicit instantiations
const float pi_float = pi<float>;   // Generates a float variable
const double pi_double = pi<double>; // Generates a double variable

// pi<float> and pi<double> have different memory addresses
```

## Specialization

Variable templates support both explicit (full) specialization and partial specialization, utilizing the same resolution rules as class templates.

### Explicit Specialization

You can define a completely different type and initializer for a specific template argument.

```cpp theme={"dark"}
template <typename T>
constexpr T default_value = T();

// Explicit specialization for const char*
template <>
constexpr const char* default_value<const char*> = "null-string";
```

### Partial Specialization

You can specialize the variable template for a subset of types, such as pointers or arrays.

```cpp theme={"dark"}
template <typename T>
constexpr bool is_pointer = false;

// Partial specialization for any pointer type
template <typename T>
constexpr bool is_pointer<T*> = true;
```

## Scope and Linkage

Variable templates can be declared at namespace scope or as static data members within a class. They cannot be declared at block scope (inside a function).

**Namespace Scope:**

```cpp theme={"dark"}
namespace math {
    template <typename T>
    constexpr T e = T(2.71828182845904523536L);
}
```

**Class Scope (Static Data Members):**
A non-templated class can contain a templated static data member.

```cpp theme={"dark"}
struct Limits {
    template <typename T>
    static const T minimum;
};

// Out-of-line definition
template <typename T>
const T Limits::minimum = /* implementation */;
```

## Type Deduction and Constraints

Variable templates do not support template argument deduction from the initializer; the template arguments must always be explicitly specified (e.g., `var<int>`, not `var`).

However, starting in C++20, variable templates can be constrained using Concepts to restrict the types that can be used for instantiation.

```cpp theme={"dark"}
#include <concepts>

// Constrained variable template: T must satisfy std::integral
template <std::integral T>
constexpr T zero_value = 0;

// zero_value<int> is valid.
// zero_value<double> results in a compilation error.
```

## Type Traits Equivalence

Variable templates are the underlying mechanism for the `_v` suffix conventions found in the `<type_traits>` library. They act as syntactic sugar over accessing the `::value` static member of a class template.

```cpp theme={"dark"}
// Traditional class template approach (C++11)
bool b1 = std::is_same<int, int>::value;

// Variable template equivalent (C++17)
bool b2 = std::is_same_v<int, int>;

// Internal implementation of the variable template
template <typename T, typename U>
inline constexpr bool is_same_v = is_same<T, U>::value;
```

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