std::tuple_size
Defined in header <tuple>
|
||
Defined in header <array>
|
||
Defined in header <utility>
|
||
Defined in header <ranges>
|
(since C++20) |
|
template< class T > struct tuple_size; // not defined |
(1) | (since C++11) |
template< class T > struct tuple_size< const T > |
(2) | (since C++11) |
template< class T > struct tuple_size< volatile T > |
(3) | (since C++11) (deprecated in C++20) |
template< class T > struct tuple_size< const volatile T > |
(4) | (since C++11) (deprecated in C++20) |
Provides access to the number of elements in a tuple-like type as a compile-time constant expression.
value
from the corresponding cv-unqualified versions by default.
(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 #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 |
(since C++17) |
Contents |
Specializations
The standard library provides following specializations for standard library types:
(C++11) |
obtains the size of
a |
(C++11) |
obtains the size of a pair (class template specialization) |
(C++11) |
obtains the size of an array (class template specialization) |
obtains the size of a std::ranges::subrange (class template specialization) |
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.
Helper variable template
template< class T > inline constexpr std::size_t tuple_size_v = tuple_size<T>::value; |
(since C++17) | |
Inherited from std::integral_constant
Member constants
value [static] |
for a standard specialization, the number of elements in the tuple-like type T (public static member constant) |
Member functions
operator std::size_t |
converts the object to std::size_t, returns value (public member function) |
operator() (C++14) |
returns value (public member function) |
Member types
Type | Definition |
value_type
|
std::size_t |
type
|
std::integral_constant<std::size_t, value> |
Example
This section is incomplete Reason: no example |
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 |
See also
Structured binding (C++17) | binds the specified names to sub-objects or tuple elements of the initializer |
(C++11) |
obtains the element types of a tuple-like type (class template) |
(C++11) |
creates a tuple by concatenating any number of tuples (function template) |