std::make_pair(3)

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

πŸ’… NAME πŸ’…

okay bestie, welcome to the std::make_pair(3) page β€” that's a C++ Standard Library man page, and today's main character is literally std::make_pair. yes the NAME section is just the function name twice, she's humble like that πŸŽ€


✨ Synopsis ✨

first things first, she lives in <utility>, so no cap you gotta include that header:

Defined in header <utility>

here's the glow-up timeline of her signatures (this is the template< class T1, class T2 > era, bestie):

template< class T1, class T2 >                  (until C++11)
std::pair<T1, T2> make_pair( T1 t, T2 u );
template< class T1, class T2 >                  (since C++11)
std::pair<V1, V2> make_pair( T1&& t, T2&& u );  (constexpr since C++14)

so what does she do? she creates a std::pair object, and she deduces the target type from the types of the arguments. she's not gonna make you spell it out, that's the whole vibe πŸ’…

now the tea on the deduced types V1 and V2 β˜• they are std::decay<T1>::type and std::decay<T2>::type β€” those are the usual type transformations applied to arguments of functions passed by value β€” unless (since C++11) applying std::decay results in std::reference_wrapper<X> for some type X, in which case the deduced type is X&. yep, that's the little plot twist, iykyk 🌸


🫢 Parameters 🫢

  • t, u β€” the values to construct the pair from. that's it, that's the whole cast πŸŽ€

πŸ‘‘ Return value πŸ‘‘

a std::pair object containing the given values. she gives you exactly what you asked for, iconic behavior, no notes ⭐


🌈 Example 🌈

run this code, bestie, i promise it slaps:

// Run this code

#include <functional>
#include <iostream>
#include <utility>

int main()
{
    int n = 1;
    int a[5] = {1, 2, 3, 4, 5};

    // build a pair from two ints
    auto p1 = std::make_pair(n, a[1]);
    std::cout << "The value of p1 is "
              << '(' << p1.first << ", " << p1.second << ")\n";

    // build a pair from a reference to int and an array (decayed to pointer)
    auto p2 = std::make_pair(std::ref(n), a);
    n = 7;
    std::cout << "The value of p2 is "
              << '(' << p2.first << ", " << *(p2.second + 2) << ")\n";
}

🌟 Output

The value of p1 is (1, 2)
The value of p2 is (7, 3)

see?? p1 just took the values, but p2 is out here being a whole reference situation β€” that's why the 7 shows up after n = 7, and the array decayed to a pointer so *(p2.second + 2) is doing the math for you 🧚


πŸ’€ Defect reports πŸ’€

the following behavior-changing defect reports were applied retroactively to previously published C++ standards. yes bestie, retroactively, like editing the group chat receipts 😭

  • LWG 181 β€” applied to C++98. behavior as published: the parameter types were const-reference types, which made passing arrays impossible. correct behavior: changed these types to value types. we love a glow-up fix πŸ”₯
ngl if you're on ancient C++98 and trying to pass an array, this is why it wasn't vibing. the fix is the value types. slay.

πŸ’– okay bestie, that's the whole page β€” go make some pairs. this one came straight from http://cppreference.com, version 2024.06.10, manual page std::make_pair(3). drink some water, hydrate, and may all your types decay correctly πŸ’œπŸ¦‹βœ¨

πŸ“¦ where this page comes from