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.

The sizeof... operator is a compile-time operator introduced in C++11 that yields the number of elements contained within a template parameter pack. It evaluates to a constant expression of type std::size_t and is exclusively used within the context of variadic templates. Unlike the standard sizeof operator, which computes the memory footprint of a type or expression in bytes, sizeof... strictly calculates the arity (the total count of arguments) of a pack.

Syntax

sizeof...(parameter_pack)
The parameter_pack identifier can refer to either a template parameter pack or a function parameter pack.

Technical Characteristics

  • Compile-Time Evaluation: Because the size of a parameter pack is strictly known at compile time, sizeof... always yields a constant expression (specifically, a prvalue of type std::size_t). This allows its result to be used in template arguments, array bounds, static_assert declarations, and if constexpr conditions.
  • Pack Types: The operator is agnostic to the nature of the pack. It applies equally to:
    • Type parameter packs (e.g., typename... Types)
    • Non-type parameter packs (e.g., int... Values)
    • Template template parameter packs (e.g., template<typename> class... Templates)
  • Zero-Length Packs: If the parameter pack is empty, sizeof... evaluates to 0.

Syntax Visualization

1. Type Parameter Packs When applied to a template type parameter pack, the operator counts the number of types provided during template instantiation.
#include <cstddef>

template <typename... Types>
struct TypePackInfo {
    // Evaluates to the number of types in 'Types'
    static constexpr std::size_t count = sizeof...(Types); 
};

// Example instantiation: TypePackInfo<int, double, char>::count == 3
2. Function Parameter Packs When applied to a function parameter pack, the operator counts the number of arguments passed to the function. The result is identical to querying the template parameter pack directly.
#include <cstddef>

template <typename... Args>
void evaluate_arguments(Args... args) {
    // Both expressions yield the exact same std::size_t value
    constexpr std::size_t type_count = sizeof...(Args);
    constexpr std::size_t arg_count  = sizeof...(args); 
}
3. Non-Type Parameter Packs (C++17) When applied to a non-type parameter pack, the operator counts the number of compile-time values provided. Note that the use of auto for non-type template parameters is a C++17 feature; developers targeting C++11 or C++14 must specify explicit types (e.g., int... Values) instead of auto....
#include <cstddef>

template <auto... Values>
struct ValuePackInfo {
    // Evaluates to the number of values in 'Values'
    static constexpr std::size_t count = sizeof...(Values);
};

// Example instantiation: ValuePackInfo<1, 2, 3, 4>::count == 4
Master C++ with Deep Grasping Methodology!Learn More