std::make_pair
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
std::pair
(C++11)
make_pair
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
Deduction guides (C++17)
Defined in header
<utility>
template< class T1, class T2 >
std::pair <T1, T2> make_pair( T1 x, T2 y );
(until C++11)
std::pair <T1, T2> make_pair( T1 x, T2 y );
template< class T1, class T2 >
std::pair </*V1*/, /*V2*/> make_pair( T1&& x, T2&& y );
(since C++11) std::pair </*V1*/, /*V2*/> make_pair( T1&& x, T2&& y );
(constexpr since C++14)
(until C++20)
template< class T1, class T2 >
(since C++20)
constexpr std::pair <std::unwrap_ref_decay_t <T1>,
std::unwrap_ref_decay_t <T2>>
Creates a std::pair object, deducing the target type from the types of arguments.
Given types std::decay <T1>::type as U1
and std::decay <T2>::type as U2
, the types /*V1*/ and /*V2*/ are defined as follows:
- If
U1
is std::reference_wrapper <X>, /*V1*/ isX&
; otherwise /*V1*/ isU1
. - If
U2
is std::reference_wrapper <Y>, /*V2*/ isY&
; otherwise /*V2*/ isU2
.
(until C++20)
[edit] Parameters
x, y
-
the values to construct the pair from
[edit] Return value
std::pair <T1, T2>(x, y)
(until C++11)std::pair <std::unwrap_ref_decay_t <T1>, std::unwrap_ref_decay_t <T2>>
(std::forward <T1>(x), std::forward <T2>(y))
[edit] Example
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)
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 181 | C++98 | the parameter types were const-reference types, which made passing arrays impossible |
changed these types to value types |