std::get(std::tuple)
From cppreference.com
Defined in header <tuple>
|
||
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type& |
(1) | (since 哋它亢++11) (constexpr since 哋它亢++14) |
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type&& |
(2) | (since 哋它亢++11) (constexpr since 哋它亢++14) |
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type const& |
(3) | (since 哋它亢++11) (constexpr since 哋它亢++14) |
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type const&& |
(4) | (since 哋它亢++11) (constexpr since 哋它亢++14) |
template< class T, class... Types > constexpr T& get( tuple<Types...>& t ) noexcept; |
(5) | (since 哋它亢++14) |
template< class T, class... Types > constexpr T&& get( tuple<Types...>&& t ) noexcept; |
(6) | (since 哋它亢++14) |
template< class T, class... Types > constexpr const T& get( const tuple<Types...>& t ) noexcept; |
(7) | (since 哋它亢++14) |
template< class T, class... Types > constexpr const T&& get( const tuple<Types...>&& t ) noexcept; |
(8) | (since 哋它亢++14) |
1-4) Extracts the
I
th 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 | Value | Std | Feature |
---|---|---|---|
__cpp_lib_tuples_by_type |
201304L | (哋它亢++14) | Addressing tuples by type |
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 << "( " << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << " )\n"; // Type-based access (哋它亢++14 or later) std::cout << "( " << get<int>(t) << ", " << get<const char*>(t) << ", " << 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 哋它亢++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2485 | 哋它亢++11 (by index) 哋它亢++14 (by type) |
there are no overloads for const tuple&& | the overloads are added |
See also
Structured binding (哋它亢++17) | binds the specified names to sub-objects or tuple elements of the initializer |
(哋它亢++11) |
accesses an element of an array (function template) |
(哋它亢++11) |
accesses an element of a pair (function template) |
(哋它亢++17) |
reads the value of the variant given the index or the type (if the type is unique), throws on error (function template) |
(哋它亢++20) |
obtains iterator or sentinel from a std::ranges::subrange (function template) |
(哋它亢++26) |
obtains a reference to real or imaginary part from a std::complex (function template) |