Namespaces
Variants
Views
Actions

std::get(std::tuple)

From cppreference.com
< cpp‎ | utility‎ | tuple
(1)
template< std::size_t I, class... Types >

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

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

constexpr std::tuple_element_t<I, tuple<Types...> >&

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

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

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

constexpr std::tuple_element_t<I, tuple<Types...> >&&

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

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

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

constexpr std::tuple_element_t<I, tuple<Types...> >const&

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

constexpr std::tuple_element_t<I, tuple<Types...> >const&&

    get( const tuple<Types...>&& t );
(4) (since C++17)
template< class T, class... Types >
constexpr T& get(tuple<Types...>& t);
(5) (since C++14)
template< class T, class... Types >
constexpr T&& get(tuple<Types...>&& t);
(6) (since C++14)
template< class T, class... Types >
constexpr const T& get(const tuple<Types...>& t);
(7) (since C++14)
template< class T, class... Types >
constexpr const T&& get(const tuple<Types...>&& t);
(8) (since C++17)
1-4) Extracts the Ith element from the tuple. I is an integer value in [0, sizeof...(Types)).
5-8) 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

[edit] Parameters

t - tuple whose contents to extract

[edit] Return value

A reference to the selected element of t.

[edit] Exceptions

noexcept specification:  
noexcept
  

[edit] 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)

[edit] See also

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