std::tuple_size
<array>
<tuple>
<utility>
<ranges>
<complex>
struct tuple_size; // not defined
struct tuple_size< const T >
struct tuple_size< volatile T >
(deprecated in C++20)
struct tuple_size< const volatile T >
(deprecated in C++20)
Provides access to the number of elements in a tuple-like type as a compile-time constant expression.
std::tuple_size
interacts with the core language: it can provide structured binding support in the tuple-like case.
(2-4) are SFINAE-friendly: if std::tuple_size<T>::value is
ill-formed when treated as an unevaluated operand, they do not provide the member value. Access checking is performed as if in a context unrelated to tuple_size
and T
. Only the validity of the immediate context of the expression is considered. This allows
#include <utility> struct X { int a, b; }; const auto [x, y] = X(); // structured binding declaration first attempts // tuple_size<const X> which attempts to use tuple_size<X>::value, // then soft error encountered, binds to public data members
Contents
[edit] Specializations
The standard library provides following specializations for standard library types:
a tuple
(class template specialization) [edit]
All specializations of std::tuple_size
satisfy UnaryTypeTrait with base characteristic std::integral_constant <std::size_t, N> for some N
.
Users may specialize std::tuple_size
for program-defined types to make them tuple-like. Program-defined specializations must meet the requirements above.
Usually only specialization for cv-unqualified types are needed to be customized.
[edit] Helper variable template
<tuple>
constexpr std::size_t tuple_size_v = tuple_size<T>::value;
Inherited from std::integral_constant
Member constants
T
(public static member constant)
Member functions
(public member function)
Member types
value_type
std::size_t
type
std::integral_constant <std::size_t, value>
[edit] Example
#include <array> #include <cstddef> #include <ranges> #include <tuple> #include <utility> template<class T, std::size_t Size> struct Arr { T data[Size]; }; // Program-defined specialization of std::tuple_size: template<class T, std::size_t Size> struct std::tuple_size<Arr<T, Size>> : public integral_constant<std::size_t, Size> {}; int main() { using tuple1 = std::tuple <int, char, double>; static_assert(3 == std::tuple_size_v<tuple1>); // uses using template (C++17) using array3x4 = std::array <std::array <int, 3>, 4>; static_assert(4 == std::tuple_size<array3x4>{}); // uses operator std::size_t using pair = std::pair <tuple1, array3x4>; static_assert(2 == std::tuple_size<pair>()); // uses operator() using sub = std::ranges::subrange <char*, char*>; static_assert(2 == std::tuple_size<sub>::value); using Arr5 = Arr<int, 5>; static_assert(5 == std::tuple_size_v<Arr5>); }
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2212 | C++11 | specializations for cv types were not required in some headers, which led to ambiguity | required |