std::tuple<Types...>::tuple
来自cppreference.com
<tbody>
</tbody>
| 在标头 <tuple> 定义
|
||
constexpr tuple(); |
(1) | (C++11 起) (条件性 explicit) |
tuple( const Types&... args ); |
(2) | (C++11 起) (C++14 起 constexpr) (条件性 explicit) |
template< class... UTypes > tuple( UTypes&&... args ); |
(3) | (C++11 起) (C++14 起 constexpr) (条件性 explicit) |
template< class... UTypes > constexpr tuple( tuple<UTypes...>& other ); |
(4) | (C++23 起) (条件性 explicit) |
template< class... UTypes > tuple( const tuple<UTypes...>& other ); |
(5) | (C++11 起) (C++14 起 constexpr) (条件性 explicit) |
template< class... UTypes > tuple( tuple<UTypes...>&& other ); |
(6) | (C++11 起) (C++14 起 constexpr) (条件性 explicit) |
template< class... UTypes > constexpr tuple( const tuple<UTypes...>&& other ); |
(7) | (C++23 起) (条件性 explicit) |
template< class U1, class U2 > constexpr tuple( std::pair<U1, U2>& p ); |
(8) | (C++23 起) (条件性 explicit) |
template< class U1, class U2 > tuple( const std::pair<U1, U2>& p ); |
(9) | (C++11 起) (C++14 起 constexpr) (条件性 explicit) |
template< class U1, class U2 > tuple( std::pair<U1, U2>&& p ); |
(10) | (C++11 起) (C++14 起 constexpr) (条件性 explicit) |
template< class U1, class U2 > constexpr tuple( const std::pair<U1, U2>&& p ); |
(11) | (C++23 起) (条件性 explicit) |
template< tuple-like UTuple > constexpr tuple( UTuple&& u ); |
(12) | (C++23 起) (条件性 explicit) |
tuple( const tuple& other ) = default; |
(13) | (C++11 起) |
tuple( tuple&& other ) = default; |
(14) | (C++11 起) |
| 扩展分配器构造函数 |
||
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a ); |
(15) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const Types&... args ); |
(16) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, UTypes&&... args ); |
(17) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc, class... UTypes > constexpr tuple( std::allocator_arg_t, const Alloc& a, tuple<UTypes...>& other ); |
(18) | (C++23 起) (条件性 explicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, const tuple<UTypes...>& other ); |
(19) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, tuple<UTypes...>&& other ); |
(20) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc, class... UTypes > constexpr tuple( std::allocator_arg_t, const Alloc& a, const tuple<UTypes...>&& other ); |
(21) | (C++23 起) (条件性 explicit) |
template< class Alloc, class U1, class U2 > constexpr tuple( std::allocator_arg_t, const Alloc& a, std::pair<U1, U2>& p ); |
(22) | (C++23 起) (条件性 explicit) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, const std::pair<U1, U2>& p ); |
(23) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, std::pair<U1, U2>&& p ); |
(24) | (C++11 起) (C++20 起 constexpr) (条件性 explicit) |
template< class Alloc, class U1, class U2 > constexpr tuple( std::allocator_arg_t, const Alloc& a, const std::pair<U1, U2>&& p ); |
(25) | (C++23 起) (条件性 explicit) |
template< class Alloc, tuple-like UTuple > constexpr tuple( std::allocator_arg_t, const Alloc& a, UTuple&& u ); |
(26) | (C++23 起) (条件性 explicit) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const tuple& other ); |
(27) | (C++11 起) (C++20 起 constexpr) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, tuple&& other ); |
(28) | (C++11 起) (C++20 起 constexpr) |
构造新的元组。
在以下描述中,令
i从范围[0,sizeof...(Types))中依序取值,Ti为Types中的第i个类型,以及Ui为名字是UTypes的模板形参包中的第i个类型,
其中索引基于零。
1) 默认构造函数。值初始化各元素(若存在)。如果
sizeof...(Types) == 0,那么默认构造函数平凡。
- 此重载只有在
std::is_default_constructible<Ti>::value对所有i都是true时才会参与重载决议。 - 此构造函数当且仅当至少有一个
i对应的Ti不可从{}复制列表初始化时是explicit的。
2) 直接构造函数。以对应参数初始化元组的每个元素。
- 此重载只有在
sizeof...(Types) >= 1且std::is_copy_constructible<Ti>::value对所有i都是true时才会参与重载决议。 - 此构造函数当且仅当
std::is_convertible<const Ti&, Ti>::value对至少一个i是false时是explicit的。
3) 转换构造函数。以
std::forward<UTypes>(args) 中的对应值初始化元组的每个元素。
- 此重载只有在
sizeof...(Types) == sizeof...(UTypes),且sizeof...(Types) >= 1,且std::is_constructible<Ti, Ui>::value对所有i都是true,且- 令
D为std::decay<U0>::type(C++20 前)std::remove_cvref_t<U0>(C++20 起),那么- 如果
sizeof...(Types) == 1,那么D不是std::tuple,否则, - 如果
sizeof...(Types) == 2或sizeof...(Types) == 3,那么要么D不是 std::allocator_arg_t,要么T0是 std::allocator_arg_t
- 如果
时才会参与重载决议。
- 此构造函数当且仅当
std::is_convertible<Ui, Ti>::value对至少一个i是false时是explicit的。
|
(C++23 起) |
4-7) 转换构造函数。以
other 中的对应元素初始化元组的每个元素。
正式而言,令 FWD(other) 为 std::forward<decltype(other)>(other),对于所有 i,以 std::get<i>(FWD(other)) 初始化元组的第 i 个元素。
- 此重载只有在
sizeof...(Types) == sizeof...(UTypes),std::is_constructible_v<TI, decltype(std::get<i>(FWD(other)))>对所有i都是true,且- 以下之一成立:
sizeof...(Types)不是1,或者- (当
Types...展开成T且UTypes...展开成U时)std::is_convertible_v<decltype(other), T>,std::is_constructible_v<T, decltype(other)>和std::is_same_v<T, U>都是false
时才会参与重载决议。
- 此构造函数当且仅当
std::is_convertible_v<decltype(std::get<I>(FWD(other))), TI>对至少一个i是false时是explicit的。
|
(C++23 起) |
8-11) 对偶构造函数。构造 2 个元素的元组,以
p 中的对应元素初始化元组的每个元素。
正式而言,令 FWD(p) 为 std::forward<decltype(p)>(p),从 std::get<0>(FWD(p)) 构造第一个元素,从 std::get<1>(FWD(p)) 构造第二个元素。
- 此重载只有在
sizeof...(Types) == 2,std::is_constructible_v<T0, decltype(std::get<0>(FWD(p)))>是true,且std::is_constructible_v<T1, decltype(std::get<1>(FWD(p)))>是true
时才会参与重载决议。
- 此构造函数当且仅当
std::is_convertible_v<decltype(std::get<0>(FWD(p))), T0>或std::is_convertible_v<decltype(std::get<1>(FWD(p))), T1>是false时是explicit的。
|
(C++23 起) |
12)
tuple-like 构造函数。构造元组,以 u 中的对应元素初始化元组的每个元素。
正式而言,对于所有 i,以 std::get<i>(std::forward<UTuple>(u)) 初始化元组的第 i 个元素。
- 此重载只有在
std::same_as<std::remove_cvref_t<UTuple>, std::tuple>是false,std::remove_cvref_t<UTuple>不是 std::ranges::subrange 的特化,sizeof...(Types)等于std::tuple_size_v<std::remove_cvref_t<UTuple>>,std::is_constructible_v<Ti, decltype(std::get<i>(std::forward<UTuple>(u)))>对于所有i都是true,且- 以下之一成立:
sizeof...(Types)不是1,或者- (当
Types...展开成T时)std::is_convertible_v<UTuple, T>和std::is_constructible_v<T, UTuple>都是false。时才会参与重载决议
- 如果有任何引用元素可以绑定到临时对象,则此构造函数被定义为弃置的。
13) 隐式定义的复制构造函数。以
other 的对应元素初始化元组的每个元素。
- 如果此函数进行的每个操作都是
constexpr,那么它是constexpr的。对于空元组std::tuple<>,它是constexpr的。 std::is_copy_constructible<Ti>::value必须对所有i都是true,否则行为未定义(C++20 前)此重载不会参与重载决议(C++20 起)。
14) 隐式定义的移动构造函数。对于所有
i,以 std::forward<Ui>(std::get<i>(other)) 构造元组的第 i 个元素。
- 如果此函数进行的每个操作都是
constexpr的,那么它是constexpr的。对于空元组std::tuple<>,它是constexpr的。 std::is_move_constructible<Ti>::value必须对所有i都是true,否则行为未定义(C++20 前)此重载不参与重载决议(C++20 起)。
15-28) 等同于 (1-14),但以使用分配器构造 创建每个元素,即对每个
std::uses_allocator<Ti, Alloc>::value 为 true 的元素,以分配器对象 a 为额外实参传递给其构造函数。参数
| args | - | 用于初始化元组每个元素的值 |
| other | - | 用于初始化元组每个元素的值的元组 |
| p | - | 用于初始化 2-元组的两个元素的值的对偶 |
| u | - | 用于初始化元组每个元素的值的 tuple-like 对象
|
| a | - | 用于使用分配器构造的分配器 |
注解
条件性显式的构造函数使得可以在复制初始化语境中用列表初始化语法来构造元组:
std::tuple<int, int> foo_tuple()
{
// return {1, -1}; // N4387 前错误
return std::make_tuple(1, -1); // 始终可行
}注意如果列表中某元素不可隐式转换成目标元组中的对应元素,那么构造函数会变成显式的:
using namespace std::chrono;
void launch_rocket_at(std::tuple<hours, minutes, seconds>);
launch_rocket_at({hours(1), minutes(2), seconds(3)}); // OK
launch_rocket_at({1, 2, 3}); // 错误:int 不可隐式转换到 duration
launch_rocket_at(std::tuple<hours, minutes, seconds>{1, 2, 3}); // OK示例
运行此代码
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <vector>
// 打印 vector 到流的辅助函数
template<class Os, class T>
Os& operator<<(Os& os, std::vector<T> const& v)
{
os << '{';
for (auto i{v.size()}; const T& e : v)
os << e << (--i ? "," : "");
return os << '}';
}
template<class T>
void print_single(T const& v)
{
if constexpr (std::is_same_v<T, std::decay_t<std::string>>)
std::cout << std::quoted(v);
else if constexpr (std::is_same_v<std::decay_t<T>, char>)
std::cout << "'" << v << "'";
else
std::cout << v;
}
// 打印任意大小元组的辅助函数
template<class Tuple, std::size_t N>
struct TuplePrinter
{
static void print(const Tuple& t)
{
TuplePrinter<Tuple, N - 1>::print(t);
std::cout << ", ";
print_single(std::get<N - 1>(t));
}
};
template<class Tuple>
struct TuplePrinter<Tuple, 1>
{
static void print(const Tuple& t)
{
print_single(std::get<0>(t));
}
};
template<class... Args>
void print(std::string_view message, const std::tuple<Args...>& t)
{
std::cout << message << " (";
TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
std::cout << ")\n";
}
// 辅助函数结束
int main()
{
std::tuple<int, std::string, double> t1;
print("值初始化,t1:", t1);
std::tuple<int, std::string, double> t2{42, "Test", -3.14};
print("通过值进行初始化,t2:", t2);
std::tuple<char, std::string, int> t3{t2};
print("隐式转换,t3:", t3);
std::tuple<int, double> t4{std::make_pair(42, 3.14)};
print("从 pair 构造,t4:", t4);
// 给定拥有单实参构造函数的分配器 my_alloc my_alloc(int)
// 用 my_alloc(1) 在 vector 中分配 5 个 int
using my_alloc = std::allocator<int>;
std::vector<int, my_alloc> v{5, 1, my_alloc{/* 1 */}};
// 用 my_alloc(2) 在 tuple 中的 vector 中分配 5 个 int
std::tuple<int, std::vector<int, my_alloc>, double> t5
{std::allocator_arg, my_alloc{/* 2 */}, 42, v, -3.14};
print("带分配器构造,t5:", t5);
}可能的输出:
值初始化,t1:(0, "", 0)
通过值进行初始化,t2:(42, "Test", -3.14)
隐式转换,t3:('*', "Test", -3)
从 pair 构造,t4:(42, 3.14)
带分配器构造,t5:(42, {1,1,1,1,1}, -3.14)缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2510 | C++11 | 默认构造函数是隐式的 | 使之为条件性 explicit |
| LWG 3121 | C++11 | 1-tuple 的默认构造函数可能递归地检查约束;allocator_arg_t 参数带来歧义
|
进一步约束构造函数 |
| LWG 3158 | C++11 | 对应默认构造函数的使用分配器构造函数为隐式 | 使之为条件性 explicit |
| LWG 3211 | C++11 | 未指定 tuple<> 的默认构造函数是否为平凡
|
要求为平凡 |
| LWG 4045 | C++23 | tuple-like 构造函数可能潜在地创建悬垂引用
|
将其定义为弃置 |
| N4387 | C++11 | 某些构造函数曾经是 explicit 的,阻止了有用的行为 | 使大多数构造函数为条件性 explicit |
参阅
赋值一个 tuple 的内容给另一个 (公开成员函数) | |
(C++11) |
创建一个 tuple 对象,其类型根据各实参类型定义 (函数模板) |
(C++11) |
创建左值引用的 std::tuple,或将元组解包为独立对象 (函数模板) |
(C++11) |
创建转发引用的 tuple (函数模板) |
构造新的 pair ( std::pair<T1,T2> 的公开成员函数)
|