std::get(std::pair)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 to C++11 (by index) and C++14 (by type); behavior as published: there are no overloads for const 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 a std::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