std::get(std::tuple)(3)

NAME
std::get(std::tuple) - std::get(std::tuple)

Synopsis
   Defined in header <tuple>
   template< std::size_t I, class... Types >
                        (since C++11)
   typename std::tuple_element<I, tuple<Types...> >::type& (1) (constexpr since C++14)
   get( tuple<Types...>& t ) noexcept;
   template< std::size_t I, class... Types >
                        (since C++11)
   typename std::tuple_element<I, tuple<Types...> >::type&& (2) (constexpr since C++14)
   get( tuple<Types...>&& t ) noexcept;
   template< std::size_t I, class... Types >
   typename std::tuple_element<I, tuple<Types...> >::type (3) (since C++11)
   const& (constexpr since C++14)
   get( const tuple<Types...>& t ) noexcept;
   template< std::size_t I, class... Types >
   typename std::tuple_element<I, tuple<Types...> >::type (4) (since C++11)
   const&& (constexpr since C++14)
   get( const tuple<Types...>&& t ) noexcept;
   template< class T, class... Types > (5) (since C++14)
   constexpr T& get(tuple<Types...>& t) noexcept;
   template< class T, class... Types > (6) (since C++14)
   constexpr T&& get(tuple<Types...>&& t) noexcept;
   template< class T, class... Types >
   constexpr const T& get(const tuple<Types...>& t) (7) (since C++14)
   noexcept;
   template< class T, class... Types >
   constexpr const T&& get(const tuple<Types...>&& t) (8) (since C++14)
   noexcept;
   1-4) Extracts the Ith element from the tuple. I must be an integer value in [0,
   sizeof...(Types)).
   5-8) Extracts the element of the tuple t whose type is T. Fails to compile unless
   the tuple has exactly one element of that type.

Parameters
   t - tuple whose contents to extract

Return value
   A reference to the selected element of t.

Notes
   Feature-test macro: __cpp_lib_tuples_by_type (for type-based access)

Example
// Run this code
 #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 (C++14 or later)
     std::cout << "(" << std::get<int>(t) << ", " << std::get<const char*>(t)
               << ", " << std::get<double>(t) << ")\n";
     // Note: std::tie and structured binding may also be used to decompose a tuple
 }

Output:
 (1, Foo, 3.14)
 (1, Foo, 3.14)
  Defect reports
   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.
      DR Applied to Behavior as published Correct behavior
   LWG 2485 C++11 (by index) there are no overloads for const the overloads are added
            C++14 (by type) tuple&&

See also
   Structured binding (C++17) binds the specified names to sub-objects or tuple
                        elements of the initializer
   std::get(std::array) accesses an element of an array
   (C++11) (function template)
   std::get(std::pair) accesses an element of a pair
   (C++11) (function template)
   std::get(std::variant) reads the value of the variant given the index or the
   (C++17) type (if the type is unique), throws on error
                        (function template)
   get(std::ranges::subrange) obtains iterator or sentinel from a std::ranges::subrange
   (C++20) (function template)