std::make_pair
From cppreference.com
| ヘッダ <utility> で定義 |
||
| template< class T1, class T2 > std::pair<T1, T2> make_pair( T1 x, T2 y ); |
(C++11まで) | |
template< class T1, class T2 > std::pair</*V1*/, /*V2*/> make_pair( T1&& x, T2&& y ); |
(C++11以降) (C++14以降constexpr) (C++20まで) |
|
| template< class T1, class T2 > constexpr std::pair<std::unwrap_ref_decay_t<T1>, |
(C++20以降) | |
引数からターゲット型を推論して、std::pair オブジェクトを作成します。
|
型 std::decay<T1>::type を
|
(C++11以降) (C++20まで) |
目次 |
[編集] パラメータ
| x, y | - | ペアを構築するための値 |
[編集] 戻り値
|
std::pair<T1, T2>(x, y) |
(C++11まで) |
|
std::pair</*V1*/, /*V2*/>(std::forward<T1>(x), std::forward<T2>(y)) |
(C++11以降) (C++20まで) |
|
std::pair<std::unwrap_ref_decay_t<T1>, std::unwrap_ref_decay_t<T2>> |
(C++20以降) |
[編集] 例
このコードを実行
#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"; }
出力
The value of p1 is (1, 2) The value of p2 is (7, 3)
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 181 | C++98 | パラメータ型が const 参照でした 配列の受け渡しができなくなるため |
これらを変更しました 値型に |