std::get(std::pair)(3)
π std::get(std::pair) π
okay so welcome to the manual page for std::get(std::pair) β that's the C++ Standard Library one, bestie, page std::get(std::pair)(3). It lives in the header <utility> and it is, genuinely, one of the most useful little gremlins in the whole standard. Let's get into it π
β¨ NAME β¨
std::get(std::pair) β which is, and I cannot stress this enough, std::get(std::pair). That's the name. That's the whole name. Short king energy π
πΈ SYNOPSIS πΈ
Defined in header <utility>.
Overload (1) β since C++11, constexpr since C++14:
template< std::size_t I, class T1, class T2 >
typename std::tuple_element<I, std::pair<T1,T2>>::type&
get( std::pair<T1, T2>& p ) noexcept;
Overload (2) β since C++11, constexpr since C++14:
template< std::size_t I, class T1, class T2 >
const typename std::tuple_element<I, std::pair<T1,T2>>::type&
get( const std::pair<T1,T2>& p ) noexcept;
Overload (3) β since C++11, constexpr since C++14:
template< std::size_t I, class T1, class T2 >
typename std::tuple_element<I, std::pair<T1,T2>>::type&&
get( std::pair<T1,T2>&& p ) noexcept;
Overload (4) β since C++11, constexpr since C++14:
template< std::size_t I, class T1, class T2 >
const typename std::tuple_element<I, std::pair<T1,T2>>::type&&
get( const std::pair<T1,T2>&& p ) noexcept;
Overload (5) β since C++14:
template< class T, class U >
constexpr T& get( std::pair<T, U>& p ) noexcept;
Overload (6) β since C++14:
template< class T, class U >
constexpr const T& get( const std::pair<T, U>& p ) noexcept;
Overload (7) β since C++14:
template< class T, class U >
constexpr T&& get( std::pair<T, U>&& p ) noexcept;
Overload (8) β since C++14:
template< class T, class U >
constexpr const T&& get( const std::pair<T, U>&& p ) noexcept;
Overload (9) β since C++14:
template< class T, class U >
constexpr T& get( std::pair<U, T>& p ) noexcept;
Overload (10) β since C++14:
template< class T, class U >
constexpr const T& get( const std::pair<U, T>& p ) noexcept;
Overload (11) β since C++14:
template< class T, class U >
constexpr T&& get( std::pair<U, T>&& p ) noexcept;
Overload (12) β since C++14:
template< class T, class U >
constexpr const T&& get( const std::pair<U, T>&& p ) noexcept;
π¦ DESCRIPTION π¦
Extracts an element from the pair using tuple-like interface. Yes bestie, std::pair really said "I too can be indexed like a tuple" and honestly? Slay.
1β4) The index-based overloads fail to compile if the index I is neither 0 nor 1. That's it, that's the whole vibe check β pair only has two slots, so std::get<2> is not happening, no cap π
5β12) The type-based overloads fail to compile if the types T and U are the same. Like, if both slots are the same type, girl, which one do you want?? Ambiguous. The compiler cannot read your mind π
π PARAMETERS π
pβ pair whose contents to extract
π RETURN VALUE π
1β4) Returns a reference to p.first if I == 0 and a reference to p.second if I == 1.
5β8) Returns a reference to p.first.
9β12) Returns a reference to p.second.
The way the type-based ones just go "first" for 5β8 and "second" for 9β12 based on the order of T and U in the std::pair<...> β that's the trick, iykyk π§
π₯ EXAMPLE π₯
// Run this code
#include <iostream>
#include <utility>
int main()
{
auto p = std::make_pair(1, 3.14);
std::cout << '(' << std::get<0>(p) << ", " << std::get<1>(p) << ")\n";
std::cout << '(' << std::get<int>(p) << ", " << std::get<double>(p) << ")\n";
}
Output:
(1, 3.14)
(1, 3.14)
Both ways give you the same thing β index-based and type-based, matching, periodt β¨
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Retroactive glow-up, we love to see it π
LWG 2485β applied toC++11(by index) andC++14(by type); behavior as published: there are no overloads forconst pair&&; 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::tuple)(C++11) β tuple accesses specified element (function template)get(std::array)(C++11) β accesses an element of an array (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)
okay that's the whole page, bestie, you ate π«Ά source is http://cppreference.com, version date 2024.06.10, and this was std::get(std::pair)(3) β go forth and index your pairs 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