std::tuple<Types...>::tuple

来自cppreference.com
< cpp‎ | utility‎ | tuple
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
初等字符串转换
(C++17)
(C++17)
 
 
在标头 <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)
tuple( const tuple& other ) = default;
(12) (C++11 起)
tuple( tuple&& other ) = default;
(13) (C++11 起)
template< class Alloc >
tuple( std::allocator_arg_t, const Alloc& a );
(14) (C++11 起)
(C++20 起为 constexpr)
(条件性 explicit)
template< class Alloc >

tuple( std::allocator_arg_t, const Alloc& a,

       const Types&... args );
(15) (C++11 起)
(C++20 起为 constexpr)
(条件性 explicit)
template< class Alloc, class... UTypes >

tuple( std::allocator_arg_t, const Alloc& a,

       UTypes&&... args );
(16) (C++11 起)
(C++20 起为 constexpr)
(条件性 explicit)
template< class Alloc, class... UTypes >

constexpr tuple( std::allocator_arg_t, const Alloc& a,

                 tuple<UTypes...>& other );
(17) (C++23 起)
(条件性 explicit)
template< class Alloc, class... UTypes >

tuple( std::allocator_arg_t, const Alloc& a,

       const tuple<UTypes...>& other );
(18) (C++11 起)
(C++20 起为 constexpr)
(条件性 explicit)
template< class Alloc, class... UTypes >

tuple( std::allocator_arg_t, const Alloc& a,

       tuple<UTypes...>&& other );
(19) (C++11 起)
(C++20 起为 constexpr)
(条件性 explicit)
template< class Alloc, class... UTypes >

constexpr tuple( std::allocator_arg_t, const Alloc& a,

                 const tuple<UTypes...>&& other );
(20) (C++23 起)
(条件性 explicit)
template< class Alloc, class U1, class U2 >

constexpr tuple( std::allocator_arg_t, const Alloc& a,

                 std::pair<U1, U2>& p );
(21) (C++23 起)
(条件性 explicit)
template< class Alloc, class U1, class U2 >

tuple( std::allocator_arg_t, const Alloc& a,

       const std::pair<U1, U2>& p );
(22) (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 );
(23) (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 );
(24) (C++23 起)
(条件性 explicit)
template< class Alloc >

tuple( std::allocator_arg_t, const Alloc& a,

       const tuple& other );
(25) (C++11 起)
(C++20 起为 constexpr)
template< class Alloc >

tuple( std::allocator_arg_t, const Alloc& a,

       tuple&& other );
(26) (C++11 起)
(C++20 起为 constexpr)

构造新的 tuple

1) 默认构造函数。值初始化所有元素,若存在。若 sizeof...(Types) == 0 则默认构造函数为平凡。
  • 此重载只有在 std::is_default_constructible<Ti>::value 对所有 itrue 时才参与重载决议。
  • 构造函数若且唯若对至少一个 iTi 不可从 {} 复制列表初始化才为 explicit
2) 直接构造函数。以对应参数初始化 tuple 的每个元素。
  • 此重载只有在 sizeof...(Types) >= 1std::is_copy_constructible<Ti>::value 对所有 itrue 时才参与重载决议。
  • 此构造函数若且唯若 std::is_convertible<const Ti&, Ti>::value 对至少一个 ifalse 才为 explicit
3) 转换构造函数。以 std::forward<UTypes>(args) 中的对应值初始化 tuple 的每个元素。
  • 此重载只有在
    • sizeof...(Types) == sizeof...(UTypes) ,且
    • sizeof...(Types) >= 1 ,且
    • std::is_constructible<Ti, Ui>::value 对所有 itrue ,且
    • Dstd::decay<U0>::type (C++20 前)std::remove_cvref_t<U0> (C++20 起) ,则
      • sizeof...(Types) == 1D 不是 tuple ,否则,
      • sizeof...(Types) == 2sizeof...(Types) == 3 ,则要么 D 不是 std::allocator_arg_t ,要么 T0std::allocator_arg_t 时才参与重载决议。
  • 构造函数若且唯若 std::is_convertible<Ui, Ti>::value 对至少一个 ifalse 才为 explicit
(C++23 起)
4) 转换复制构造函数。对 sizeof...(UTypes) 中所有 i ,以 std::get<i>(other) 初始化 tuple 的第 i 个元素。
  • 此重载只有在
  • 构造函数若且唯若 std::is_convertible_v<Ui&, Ti> 对至少一个 ifalse 才为 explicit
  • 若任何引用元素的初始化会绑定它到临时对象,则定义此构造函数为被删除。
5) 转换复制构造函数。对 sizeof...(UTypes) 中所有 i ,以 std::get<i>(other) 初始化 tuple 的第 i 个元素。
  • 此重载只有在
    • sizeof...(Types) == sizeof...(UTypes)
    • std::is_constructible<Ti, const Ui&>::value 对所有 itrue
    • 以下之一成立
  • 构造函数若且唯若 std::is_convertible<const Ui&, Ti>::value 对至少一个 ifalse 才为 explicit
  • 若任何引用元素的初始化会绑定它到临时对象,则定义此构造函数为被删除。
(C++23 起)
6) 转换移动构造函数。对 sizeof...(UTypes) 中所有 i ,以 std::forward<Ui>(std::get<i>(other)) 初始化 tuple 的第 i 个元素。
  • 此重载只有在
  • 构造函数若且唯若 std::is_convertible<Ui, Ti>::value 对至少一个 ifalse 才为 explicit
  • 若任何引用元素的初始化会绑定它到临时对象,则定义此构造函数为被删除。
(C++23 起)
7) 转换移动构造函数。对 sizeof...(UTypes) 中所有 i ,以 std::forward<const Ui>(std::get<i>(other)) 初始化 tuple 的第 i 个元素。
  • 此重载只有在
  • 构造函数若且唯若 std::is_convertible_v<const Ui, Ti> 对至少一个 ifalse 才为 explicit
  • 若任何引用元素的初始化会绑定它到临时对象,则定义此构造函数为被删除。
8) pair 复制构造函数。构造 2-tuple ,从 p.first 构造第一个元素,从 p.second 构造第二个元素。
9) pair 复制构造函数。构造 2-tuple ,从 p.first 构造第一个元素,从 p.second 构造第二个元素。
  • 若任何引用元素的初始化会绑定它到临时对象,则定义此构造函数为被删除。
(C++23 起)
10) pair 移动构造函数。构造 2-tuple ,从 std::forward<U1>(p.first) 构造第一个元素,从 std::forward<U2>(p.second) 构造第二个元素。
  • 若任何引用元素的初始化会绑定它到临时对象,则定义此构造函数为被删除。
(C++23 起)
11) pair 移动构造函数。构造 2-tuple ,从 std::forward<const U1>(p.first) 构造第一个元素,从 std::forward<const U2>(p.second) 构造第二个元素。
12) 隐式定义的复制构造函数。以 other 的对应元素初始化 tuple 的每个元素。
  • 若此函数进行的每个操作都是 constexpr ,则它是 constexpr 。对于空 tuple std::tuple<> ,它是 constexpr
  • std::is_copy_constructible<Ti>::value 必须对所有 itrue ,否则行为未定义 (C++20 前)程序非良构 (C++20 起)
13) 隐式定义的移动构造函数。以 std::forward<Ui>(std::get<i>(other)) 构造 tuple 的第 i 个元素。
若此函数进行的每个操作都是 constexpr ,则它是 constexpr 。对于空 tuple std::tuple<> ,它是 constexpr
  • std::is_move_constructible<Ti>::value 必须对所有 itrue ,否则行为未定义 (C++20 前)此重载不参与重载决议 (C++20 起)
14-26) 等同于 (1-12) ,除了以使用分配器构造创建每个元素每个元素,即以分配器对象 a 为额外参数传递给每个 std::uses_allocator<Ui, Alloc>::valuetrue 的对象的构造函数。

参数

args - 用于初始化 tuple 每个元素的值
other - 用于初始化 tuple 每个元素的值的 tuple
p - 用于初始化此 2-tuple 的两个元素的值的 pair
a - 用于使用分配器构造的分配器

注解

条件性 explicit 的构造函数使得可以用列表初始化语法于复制初始化语境构造 tuple :

std::tuple<int, int> foo_tuple() 
{
  return {1, -1};  // N4387 前错误
  return std::make_tuple(1, -1); // 始终可行
}

注意若列表中某元素不可隐式转换成目标 tuple 中的对应元素,则构造函数成为 explicit 。

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 <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;
}
 
// 打印任何大小的 tuple 的辅助函数
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(const std::tuple<Args...>& t)
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")\n";
}
// 结束辅助函数
 
int main()
{
    std::tuple<int, std::string, double> t1;
    std::cout << "Value-initialized, t1: "; print(t1);
    std::tuple<int, std::string, double> t2{42, "Test", -3.14};
    std::cout << "Initialized with values, t2: "; print(t2);
    std::tuple<char, std::string, int> t3{t2};
    std::cout << "Implicitly converted, t3: "; print(t3);
    std::tuple<int, double> t4{std::make_pair(42, 3.14)};
    std::cout << "Constructed from a pair, t4: "; print(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};
    std::cout << "Constructed with allocator, t5: "; print(t5);
}

可能的输出:

Value-initialized, t1: (0, "", 0)
Initialized with values, t2: (42, "Test", -3.14)
Implicitly converted, t3: ('*', "Test", -3)
Constructed from a pair, t4: (42, 3.14)
Constructed with allocator, t5: (42, {1,1,1,1,1}, -3.14)

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
N4387 C++11 某些构造函数曾为 explicit ,阻止了有用的行为 使大多数构造函数为条件性 explicit
LWG 2510 C++11 默认构造函数为隐式 使之为条件性 explicit
LWG 3121 C++11 1-tuple 的默认构造函数可能递归地检查约束;
allocator_arg_t 参数带来歧义
进一步约束构造函数
LWG 3158 C++11 对应默认构造函数的使用分配器构造函数为隐式 使之为条件性 explicit
LWG 3211 C++11 未指定 tuple<> 的默认构造函数是否为平凡 要求为平凡

参阅

创建一个 tuple 对象,其类型根据各实参类型定义
(函数模板)
创建左值引用的 tuple,或将 tuple 解包为独立对象
(函数模板)
创建转发引用tuple
(函数模板)