Class Templates

Default Values for Non-Type Template Parameters

Is it possible to have default values for non-type template parameters?

Illustration representing computer hardware

Yes, it is indeed possible to have default values for non-type template parameters in C++. This feature allows you to provide a default value that will be used if the user doesn't specify a value when instantiating the template. It works similarly to default function arguments.

Here's an example demonstrating how to use default values for non-type template parameters:

#include <array>
#include <iostream>

// Template with a non-type parameter
// with a default value
template <typename T, std::size_t Size = 5>
class FixedVector {
  std::array<T, Size> data;
  std::size_t count{0};

  void Add(const T& value) {
    if (count < Size) {
      data[count++] = value;

  void Print() const {
    for (std::size_t i = 0; i < count; ++i) {
      std::cout << data[i] << ' ';
    std::cout << '\n';

  std::size_t Capacity() const { return Size; }

int main() {
  // Using default size
  FixedVector<int> vec1;
  std::cout << "Vec1 (default size): ";
  std::cout << "Capacity: "
    << vec1.Capacity() << '\n';

  // Specifying a custom size
  FixedVector<double, 3> vec2;
  // This won't be added as it exceeds capacity
  std::cout << "Vec2 (custom size): ";
  std::cout << "Capacity: "
    << vec2.Capacity() << '\n';
Vec1 (default size): 1 2 3
Capacity: 5
Vec2 (custom size): 1.1 2.2 3.3
Capacity: 3

In this example, we've created a FixedVector class template that uses a non-type template parameter Size to determine the capacity of the vector. We've given Size a default value of 5.

Key points to note:

  1. The syntax for providing a default value is similar to function default arguments: std::size_t Size = 5.
  2. When instantiating the template, you can omit the non-type argument if you're happy with the default value: FixedVector<int> will use the default size of 5.
  3. You can still specify a custom value if needed: FixedVector<double, 3> creates a vector with a capacity of 3.
  4. Default values for template parameters must be constant expressions that can be evaluated at compile-time.
  5. You can have multiple non-type template parameters with default values, just like function arguments.

Default values for non-type template parameters can make your templates more flexible and easier to use, as they allow users to opt for a sensible default behavior while still providing the option to customize when needed.

Remember that once you provide a default value for a template parameter, all subsequent parameters must also have default values. This is similar to the rule for function default arguments.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved