std::pair<T1,T2>:: pair
|
pair
(
)
;
|
(1) |
(自 C++11 起为 constexpr)
(自 C++11 起有条件显式) |
|
pair
(
const
T1
&
x,
const
T2
&
y
)
;
|
(2) |
(自 C++11 起有条件显式)
(自 C++14 起为 constexpr) |
| (3) | ||
|
template
<
class
U1,
class
U2
>
pair ( U1 && x, U2 && y ) ; |
(自 C++11 起)
(直至 C++23) (自 C++14 起为 constexpr) (有条件显式) |
|
|
template
<
class
U1
=
T1,
class
U2
=
T2
>
constexpr pair ( U1 && x, U2 && y ) ; |
(自 C++23 起)
(有条件显式) |
|
|
template
<
class
U1,
class
U2
>
constexpr pair ( pair < U1, U2 > & p ) ; |
(4) |
(自 C++23 起)
(有条件显式) |
|
template
<
class
U1,
class
U2
>
pair ( const pair < U1, U2 > & p ) ; |
(5) |
(自 C++11 起有条件显式)
(自 C++14 起为 constexpr) |
|
template
<
class
U1,
class
U2
>
pair ( pair < U1, U2 > && p ) ; |
(6) |
(自 C++14 起为 constexpr)
(自 C++11 起有条件显式) |
|
template
<
class
U1,
class
U2
>
constexpr pair ( const pair < U1, U2 > && p ) ; |
(7) |
(自 C++23 起)
(有条件显式) |
|
template
<
pair
-
like P
>
constexpr pair ( P && u ) ; |
(8) |
(自 C++23 起)
(有条件显式) |
|
template
<
class
...
Args1
,
class
...
Args2
>
pair
(
std::
piecewise_construct_t
,
|
(9) |
(自 C++11 起)
(自 C++20 起为 constexpr) |
|
pair
(
const
pair
&
p
)
=
default
;
|
(10) | |
|
pair
(
pair
&&
p
)
=
default
;
|
(11) | (自 C++11 起) |
构造一个新的 pair 对象。
first
和
second
这两个元素。
|
当且仅当 std:: is_default_constructible_v < T1 > 与 std:: is_default_constructible_v < T2 > 均为 true 时,此构造函数参与重载决议。
当且仅当
|
(since C++11) |
first
,使用
y
初始化
second
。
|
当且仅当 std:: is_copy_constructible_v < T1 > 与 std:: is_copy_constructible_v < T2 > 均为 true 时,此构造函数参与重载决议。 当且仅当 std:: is_convertible_v < const T1 & , T1 > 为 false 或 std:: is_convertible_v < const T2 & , T2 > 为 false 时,此构造函数为 explicit 。 |
(C++11 起) |
|
若
|
(since C++23) |
first
,并使用
p.
second
初始化
second
。
first
或
second
的初始化会将引用绑定到临时对象时,该构造函数被定义为删除的。
first
,并使用
p.
second
初始化
second
。
|
当且仅当 std:: is_constructible_v < T1, const U1 & > 与 std:: is_constructible_v < T2, const U2 & > 均为 true 时,此构造函数参与重载决议。 当且仅当 std:: is_convertible_v < const U1 & , T1 > 为 false 或 std:: is_convertible_v < const U2 & , T2 > 为 false 时,此构造函数为 explicit 。 |
(since C++11) |
|
若
|
(since C++23) |
first
,并使用
std::
forward
<
U2
>
(
p.
second
)
初始化
second
。
|
若
|
(since C++23) |
first
,并使用
std::
forward
<
const
U2
>
(
p.
second
)
初始化
second
。
first
或
second
的初始化会将引用绑定到临时对象时,该构造函数被定义为删除的。
U1
和
U2
。使用
u1
初始化
first
,使用
u2
初始化
second
。
- std:: remove_cvref ( P ) 不是 std::ranges::subrange 的特化,
- std:: is_constructible_v < T1, U1 > 为 true ,且
- std:: is_constructible_v < T2, U2 为 true 。
first
或
second
的初始化会将引用绑定到临时对象时,该构造函数被定义为删除的。
first
的构造函数,并将
second_args
的元素转发给
second
的构造函数。这是唯一可用于创建不可复制不可移动类型 pair 的非默认构造函数。若
first
或
second
为引用类型且绑定到临时对象,则程序非良构。
目录 |
参数
| x | - | 用于初始化该pair第一个元素的值 |
| y | - | 用于初始化该pair第二个元素的值 |
| p | - | 用于初始化该pair两个元素的值对 |
| u | - | pair-like 对象,其值用于初始化该pair的两个元素 |
| first_args | - | 用于初始化该pair第一个元素的构造函数参数元组 |
| second_args | - | 用于初始化该pair第二个元素的构造函数参数元组 |
异常
除非某个指定操作(例如元素的构造函数)抛出异常,否则不会抛出异常。
示例
#include <complex> #include <iostream> #include <string> #include <tuple> #include <utility> int main() { auto print = [](auto rem, auto const& pair) { std::cout << rem << "(" << pair.first << ", " << pair.second << ")\n"; }; std::pair<int, float> p1; print("(1) 值初始化: ", p1); std::pair<int, double> p2{42, 3.1415}; print("(2) 使用两个值初始化: ", p2); std::pair<char, int> p4{p2}; print("(4) 隐式转换: ", p4); std::pair<std::complex<double>, std::string> p6 {std::piecewise_construct, std::forward_as_tuple(0.123, 7.7), std::forward_as_tuple(10, 'a')}; print("(8) 分段构造: ", p6); }
可能的输出:
(1) 值初始化: (0, 0) (2) 使用两个值初始化: (42, 3.1415) (4) 隐式转换: (*, 3) (8) 分段构造: ((0.123,7.7), aaaaaaaaaa)
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 适用标准 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 265 | C++98 |
默认构造函数通过
T1
(
)
和
T2
(
)
分别对
first
和
second
进行拷贝初始化
(因此要求
T1
和
T2
必须满足
可复制构造
要求)
|
first
和
second
进行值初始化 |
| LWG 2510 | C++11 | 默认构造函数为隐式 | 改为有条件显式 |
| N4387 | C++11 | 部分构造函数仅支持隐式转换,限制了某些使用场景 | 构造函数改为有条件显式 |
参见
根据参数类型创建
pair
对象
(函数模板) |
|
构造新的
tuple
(
std::tuple<Types...>
的公开成员函数)
|