std::get(std::tuple)(3)
πΈ NAME πΈ
std::get(std::tuple) β std::get(std::tuple) (yes bestie, thatβs literally the name and itβs iconic).
okay so this is the std::get(std::tuple)(3) page from the C++ Standard Libary manual, and itβs the whole vibe for pulling one element out of a tuple. itβs giving βaccess by index or by typeβ and Iβm obsessed π
β¨
β¨ Synopsis β¨
Defined in header <tuple> π
here are all eight overloads, no cap, no skipping β every single one is below π₯
Overload (1) β since C++11; constexpr since C++14:
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type& get( tuple<Types...>& t ) noexcept;
Overload (2) β since C++11; constexpr since C++14:
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type&& get( tuple<Types...>&& t ) noexcept;
Overload (3) β since C++11; constexpr since C++14:
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type const& get( const tuple<Types...>& t ) noexcept;
Overload (4) β since C++11; constexpr since C++14:
template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type const&& get( const tuple<Types...>&& t ) noexcept;
Overload (5) β since C++14:
template< class T, class... Types >
constexpr T& get( tuple<Types...>& t ) noexcept;
Overload (6) β since C++14:
template< class T, class... Types >
constexpr T&& get( tuple<Types...>&& t ) noexcept;
Overload (7) β since C++14:
template< class T, class... Types >
constexpr const T& get( const tuple<Types...>& t ) noexcept;
Overload (8) β since C++14:
template< class T, class... Types >
constexpr const T&& get( const tuple<Types...>&& t ) noexcept;
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.
β οΈ warning bestie: 5-8) fails to compile unless the tuple has exactly one element of that type. like, exactly one. donβt try it with two matching types and expect a cute lil result π
π Parameters π
tβ tuple whose contents to extract π«Ά
π Return value π
A reference to the selected element of t. periodt π
π¦ Notes π¦
feature-test macro moment, lowkey important:
__cpp_lib_tuples_by_typeβ value201304L, std(C++14), feature: 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 (C++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 C++ standards. no cap, they really went back and fixed it π
- DR
LWG 2485β applied toC++11(by index) andC++14(by type). Behavior as published:there are no overloads for const tuple&&. Correct behavior:the overloads are added.
π See also π
- Structured binding
(C++17)β binds the specified names to sub-objects or tuple elements of the initializer. get(std::array)(C++11)β accesses an element of an array (function template).get(std::pair)(C++11)β accesses an element of a pair (function template).get(std::variant)(C++17)β reads the value of the variant given the index or the type (if the type is unique), throws on error (function template).get(std::ranges::subrange)(C++20)β obtains iterator or sentinel from astd::ranges::subrange(function template).get(std::complex)(C++26)β obtains a reference to real or imaginary part from astd::complex(function template).
mwah π this lil page came from http://cppreference.com, version/date 2024.06.10, and itβs std::get(std::tuple)(3) β k bye bestie, go extract some elements responsibly π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms