Namespaces
Variants
Views
Actions

std::get(std::tuple)

From cppreference.com
< cpp‎ | utility‎ | tuple
Revision as of 15:55, 1 July 2013 by P12bot (Talk | contribs)

template< std::size_t I, class... Types >

constexpr typename std::tuple_element<I, tuple<Types...> >::type&

    get( tuple<Types...>& t );
(1) (since C++11)
template< std::size_t I, class... Types >

constexpr typename std::tuple_element<I, tuple<Types...> >::type&&

    get( tuple<Types...>&& t );
(2) (since C++11)
template< std::size_t I, class... Types >

constexpr typename std::tuple_element<I, tuple<Types...> >::type const&

    get( const tuple<Types...>& t );
(3) (since C++11)
template< class T, class... Types >
constexpr T& get(tuple<Types...>& t);
(4) (since C++14)
template< class T, class... Types >
constexpr T&& get(tuple<Types...>&& t);
(5) (since C++14)
template< class T, class... Types >
constexpr const T& get(const tuple<Types...>& t);
(6) (since C++14)
1-3) Extracts the Ith element element from the tuple. I is an integer value in [0, sizeof...(Types)).
4-6) Extracts the element of the tuple t whose type is T. Fails to compile if the tuple has more than one element of that type.

Contents

Parameters

t - tuple whose contents to extract

Return value

1,4) Reference to the selected element of t.
2,5) Rvalue reference to the selected element of t, unless the element is of lvalue reference type, in which case lvalue reference is returned.
3,6) Const reference to the selected element of t.

Exceptions

noexcept specification:  
noexcept
  

Notes

The overloads 1-3 were not marked constexpr in C++11

Example

#include <iostream>
#include <string>
#include <tuple>
 
int main()
{
    auto t = std::make_tuple(1, "Foo", 3.14);
    // index-based access
    std::cout << "(" << std::get<0>(t) << ", " << std::get<1>(t)
              << ", " << std::get<2>(t) << ")\n";
    // type-based access
    std::cout << "(" << std::get<int>(t) << ", " << std::get<const char*>(t)
              << ", " << std::get<double>(t) << ")\n";
}

Output:

(1, Foo, 3.14)
(1, Foo, 3.14)

See also

accesses an element of an array
(function template) [edit]
accesses an element of a pair
(function template) [edit]