CKTIS / C++
sizeof... — number of elements in a pack
The sizeof... operator computes the number of elements in a parameter pack, not their size in bytes. Its result is an integral constant of type std::size_t; an empty pack yields 0.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
sizeof...(pack_identifier)sizeof...(pack_identifier) counts the elements in the named pack. Parentheses are required, and the identifier must name a pack, not an ordinary variable or container. Unlike sizeof, the sizeof... operator does not measure bytes.
The result is a prvalue of type std::size_t and, after instantiation, is an integral constant, useful in contexts such as static_assert. An empty pack yields 0. Formally, a sizeof... expression is a pack expansion, but its result is a single number.
In the example, Ts is a template parameter pack and args is a function parameter pack. Both have the same number of elements. count() returns 0, while count(7, 'x', true) returns 3, regardless of the sizes of the supplied types.
Examples
#include <cstddef>
#include <iostream>
template<class... Ts>
constexpr std::size_t count(const Ts&... args) {
static_assert(sizeof...(Ts) == sizeof...(args));
return sizeof...(args);
}
int main() {
static_assert(count() == 0);
static_assert(count(7, 'x', true) == 3);
std::cout << count() << '\n'
<< count(7, 'x', true) << '\n';
}
Result
0 3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.