function template
<tuple>

std::get

(1)
template <size_t I, class... Types>
typename tuple_element< I, tuple<Types...> >::type& get(tuple<Types...>& tpl) noexcept;
(2)
template <size_t I, class... Types>
typename tuple_element< I, tuple<Types...> >::type&& get(tuple<Types...>&& tpl) noexcept;
(3)
template <size_t I, class... Types>
typename tuple_element< I, tuple<Types...> >::type const& get(const tuple<Types...>& tpl) noexcept;
Get element
Returns a reference to the Ith element of tuple tpl.

Version (2), which takes an rvalue reference of a tuple as argument, applies forward to the returned element.

Version (3), which takes a const tuple as argument, returns a const reference to the element.

get can also be used on tuple-like objects like pair and array (see pair's and array's specializations).

Notice that get uses a template parameter, I, to determine which element to be gotten. Template parameters must be constexpr (compile-time const values).

Template parameters

I
Position of an element in the tuple, with 0 as the position of the first element.
size_t is an unsigned integral type.
Types
Types of the elements in the tuple (generally obtained implicitly from tpl).

Function parameters

tpl
A tuple object with more than I elements.

Return value

A reference to the element at the specified position in the tuple.
tuple_element< I, tuple<Types...> >::type is the type of the Ith element in the tuple.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// tuple's get
#include <iostream>
#include <tuple>

int main ()
{
  std::tuple<int,char> mytuple (10,'a');

  std::get<0>(mytuple) = 20;

  std::cout << "mytuple contains: ";
  std::cout << std::get<0>(mytuple) << " and " << std::get<1>(mytuple);
  std::cout << std::endl;

  return 0;
}


Output:
mytuple contains: 20 and a

See also