Skip to main content
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

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.
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.
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....
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More