|
|
Member functions | ||||
Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
get(std::tuple) | ||||
Helper concepts | ||||
(C++23) | ||||
Helper classes | ||||
(C++23) | ||||
(C++23) | ||||
Deduction guides(C++17) |
Defined in header <tuple> | ||
template<std::size_t I,class...Types> typenamestd::tuple_element<I,std::tuple<Types...>>::type& | (1) | (since C++11) (constexpr since C++14) |
template<std::size_t I,class...Types> typenamestd::tuple_element<I,std::tuple<Types...>>::type&& | (2) | (since C++11) (constexpr since C++14) |
template<std::size_t I,class...Types> consttypenamestd::tuple_element<I,std::tuple<Types...>>::type& | (3) | (since C++11) (constexpr since C++14) |
template<std::size_t I,class...Types> consttypenamestd::tuple_element<I,std::tuple<Types...>>::type&& | (4) | (since C++11) (constexpr since C++14) |
template<class T,class...Types> constexpr T& get(std::tuple<Types...>& t)noexcept; | (5) | (since C++14) |
template<class T,class...Types> constexpr T&& get(std::tuple<Types...>&& t)noexcept; | (6) | (since C++14) |
template<class T,class...Types> constexprconst T& get(conststd::tuple<Types...>& t)noexcept; | (7) | (since C++14) |
template<class T,class...Types> constexprconst T&& get(conststd::tuple<Types...>&& t)noexcept; | (8) | (since C++14) |
[
0,
sizeof...(Types))
.T
. Fails to compile unless the tuple has exactly one element of that type.Contents |
t | - | tuple whose contents to extract |
A reference to the selected element oft.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_tuples_by_type | 201304L | (C++14) | Addressing tuples by type(5-8) |
#include <cassert>#include <iostream>#include <string>#include <tuple> int main(){auto x=std::make_tuple(1,"Foo",3.14); // Index-based accessstd::cout<<"( "<< std::get<0>(x)<<", "<< std::get<1>(x)<<", "<< std::get<2>(x)<<" )\n"; // Type-based access (since C++14)std::cout<<"( "<< std::get<int>(x)<<", "<< std::get<constchar*>(x)<<", "<< std::get<double>(x)<<" )\n"; conststd::tuple<int,constint,double,double> y(1,2,6.9,9.6);constint& i1= std::get<int>(y);// OK: not ambiguousassert(i1==1);constint& i2= std::get<constint>(y);// OK: not ambiguousassert(i2==2);// const double& d = std::get<double>(y); // Error: ill-formed (ambiguous) // Note: std::tie and structured binding can be// used to unpack a tuple into individual objects.}
Output:
( 1, Foo, 3.14 )( 1, Foo, 3.14 )
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) C++14 (by type) | there are no overloads forconst tuple&& | added these overloads ((4) and(8)) |
(C++11) | accesses an element of anarray (function template)[edit] |
(C++11) | accesses an element of apair (function template)[edit] |
(C++17) | reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)[edit] |
(C++20) | obtains iterator or sentinel from astd::ranges::subrange (function template)[edit] |
(C++26) | obtains a reference to real or imaginary part from astd::complex (function template)[edit] |
(C++11) | creates atuple of lvalue references or unpacks a tuple into individual objects (function template)[edit] |
Structured binding(C++17) | binds the specified names to sub-objects or tuple elements of the initializer[edit] |