std::tuple_size<std::tuple> - cppreference.com

From cppreference.com

template<class...Types>structtuple_size<std::tuple<Types...>>:std::integral_constant<std::size_t,sizeof...(Types)>{};(since C++11)Provides access to the number of elements in a tuple as a compile-time constant expression.

Helper variable template

template<classT>constexprstd::size_ttuple_size_v=tuple_size<T>::value;(since C++17)Inherited from

std::integral_constant

Member constants

value

[static]

sizeof...(Types)
(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_typestd::size_ttypestd::integral_constant<std::size_t,value>Example

Run this code

#include<iostream>#include<tuple>template<classT>voidtest(Tvalue){inta[std::tuple_size_v<T>];// can be used at compile timestd::cout<<std::tuple_size<T>{}<<' '// or at run time<<sizeofa<<' '<<sizeofvalue<<'\n';}intmain(){test(std::make_tuple(1,2,3.14));}Possible output:

3 12 16 See also

Structured binding

(C++17)binds the specified names to sub-objects or tuple elements of the initializer

[edit]

tuple_size

(C++11)

obtains the number of elements of a

tuple-like

type
(class template)

[edit]

tuple_size

(C++26)

returns the size of reflected

tuple-like

type
(function)

[edit]

std::tuple_size<std::pair>

(C++11)

obtains the size of a pair
(class template specialization)

[edit]

std::tuple_size<std::array>

(C++11)

obtains the size of an array
(class template specialization)

[edit]

std::tuple_size<std::ranges::subrange>

(C++20)

obtains the size of a

std::ranges::subrange

(class template specialization)

[edit]

std::tuple_size<std::complex>

(C++26)

obtains the size of a

std::complex

(class template specialization)

[edit]

std::tuple_element<std::tuple>

(C++11)

obtains the type of the specified element
(class template specialization)

[edit]

get(std::tuple)

(C++11)

tuple accesses specified element
(function template)

[edit]