Namespaces
Variants
Views
Actions

std::tuple_size<std::tuple>

From cppreference.com
< cpp‎ | utility‎ | tuple
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
std::tuple
Member functions
Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Deduction guides(C++17)
Helper classes
tuple_size
 
Defined in header <tuple>
template< class... Types >

struct tuple_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.

Contents

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_type std::size_t
type std::integral_constant<std::size_t, value>

[edit] Example

#include <iostream>
#include <tuple>
 
template <class T>
void test(T value)
{
    int a[std::tuple_size_v<T>]; // can be used at compile time
 
    std::cout << std::tuple_size<T>{} << ' ' // or at run time
              << sizeof a << ' ' 
              << sizeof value << '\n';
}
 
int main()
{
    test(std::make_tuple(1, 2, 3.14));
}

Possible output:

3 12 16

[edit] See also

Structured binding (C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
obtains the number of elements of a tuple-like type
(class template) [edit]
tuple accesses specified element
(function template) [edit]