Namespaces
Variants
Views
Actions

std::tuple_size<std::tuple>

From cppreference.com
< cpp‎ | utility‎ | tuple
Revision as of 09:26, 22 March 2013 by SuperBoi45 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <tuple>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class T >
class tuple_size; /*undefined*/
</td>

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class... Types >

class tuple_size<tuple<Types...> >

 : public std::integral_constant<std::size_t, sizeof...(Types)> { };
</td>

<td > (2) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class T >

class tuple_size<const T>
 : public std::integral_constant<
             typename std::remove_cv<decltype(std::tuple_size<T>::value)>::type,

             std::tuple_size<Tp>::value> { };
</td>

<td > (3) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class T >

class tuple_size< volatile T >
 : public std::integral_constant<
             typename std::remove_cv<decltype(std::tuple_size<T>::value)>::type,

             std::tuple_size<Tp>::value> { };
</td>

<td > (4) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class T >

class tuple_size< const volatile T >
 : public std::integral_constant<
             typename std::remove_cv<decltype(std::tuple_size<T>::value)>::type,

             std::tuple_size<Tp>::value> { };
</td>

<td > (5) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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)

Member types

Type Definition
value_type std::size_t
type std::integral_constant<std::size_t, value>

Example

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

Output:

3

See also

Template:cpp/container/array/dcl list tuple size
obtains the size of a pair
(class template specialization) [edit]
tuple accesses specified element
(function template) [edit]
obtains the type of the specified element
(class template specialization) [edit]