std::tuple<Types...>:: tuple
| 
           定义于头文件
            
            
             <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,
              | (16) | (C++11 起) (C++20 起为 constexpr) (条件性 explicit) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            Alloc,
            
             class
            
            ...
            
             UTypes
            
            
             >
            
             
             tuple
             
              (
             
             
              
               std::
               
                allocator_arg_t
               
              
             
             ,
             
              const
             
             Alloc
             
              &
             
             a,
              | (17) | (C++11 起) (C++20 起为 constexpr) (条件性 explicit) | 
| 
           
            
             template
            
            
             <
            
            <span class="kw1
           
           | 
构造一个新的元组。
在以下描述中,令
- 
        
         
          i
         
        
        在范围
        
         [ 0 ,sizeof... ( Types ))内按顺序取值,
- 
        Ti表示Types中的第i个类型,且
- 
        Ui表示名为UTypes的模板参数包中的第i个类型,
 
其中索引从零开始。
- 仅当对所有 i 满足 std:: is_default_constructible < Ti > :: value 为 true 时,此重载才会参与重载决议。
- 
         当且仅当至少存在一个
         
          
           i
          
         
         使得
         Ti无法从 { } 进行复制列表初始化时,该构造函数为 explicit 。
- 仅当满足 sizeof... ( Types ) >= 1 且对所有 i 满足 std:: is_copy_constructible < Ti > :: value 为 true 时,此重载才会参与重载决议。
- 当且仅当至少存在一个 i 使得 std:: is_convertible < const Ti & , Ti > :: value 为 false 时,此构造函数为 explicit 。
- 
         此重载仅在以下条件满足时参与重载决议:
         - sizeof... ( Types ) == sizeof... ( UTypes ) ,
- sizeof... ( Types ) >= 1 ,
- 对所有 i , std:: is_constructible < Ti, Ui > :: value 为 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 。
 
- 
             若
             
              
               sizeof...
               
                (
               
               Types
               
                )
               
               
                ==
               
               
                1
               
              
             
             ,则
             
 
- 当且仅当对至少一个 i , std:: is_convertible < Ui, Ti > :: value 为 false 时,该构造函数为 explicit 。
| 
 | (C++23 起) | 
        形式化地,令
        
         
          FWD
          
           (
          
          other
          
           )
          
         
        
        为
        
         
          
           
            std::
            
             forward
            
           
          
          
           <
          
          decltype
          
           (
          
          other
          
           )
          
          
           >
          
          
           (
          
          other
          
           )
          
         
        
        ,对于所有
        
         
          i
         
        
        ,使用
        
         
          std
          
           ::
          
          
           get
          
          
           <
          
          i
          
           >
          
          
           (
          
          FWD
          
           (
          
          other
          
           )
          
          
           )
          
         
        
        初始化 tuple 的第
        
         i
        
        个元素。
       
- 
         此重载仅当满足以下所有条件时才参与重载决议:
         - sizeof... ( Types ) == sizeof... ( UTypes ) ,
- 对所有 i , std:: is_constructible_v < Ti, decltype ( std :: get < i > ( FWD ( other ) ) ) > 为 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 。
 
 
- 当且仅当对至少一个 i , std:: is_convertible_v < decltype ( std :: get < i > ( FWD ( other ) ) ) , Ti > 为 false 时,这些构造函数为 explicit 。
| 
 | (since C++23) | 
形式化地,令 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 。
| 
 | (since C++23) | 
         
          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 >> ,
- 对所有 i , std:: is_constructible_v < Ti, decltype ( std :: get < i > ( std:: forward < UTuple > ( u ) ) ) > 为 true ,且
- 
           满足以下任一条件:
           - sizeof... ( Types ) 不等于 1 ,或
- 
             (当
             Types...展开为T时) std:: is_convertible_v < UTuple, T > 和 std:: is_constructible_v < T, UTuple > 均为 false 。
 
 
- 如果任何引用元素的初始化会将其绑定到临时对象,则此构造函数被定义为已删除。
- 若该构造函数执行的每个操作均为 constexpr ,则此构造函数为 constexpr 。对于空元组 std:: tuple <> ,该构造函数为 constexpr 。
- 对所有 i , std:: is_copy_constructible < Ti > :: value 必须为 true ,否则 行为未定义 (C++20 前) 程序非良构 (C++20 起) 。
        i
       
       个元素。
       - 若该构造函数执行的所有操作均为 constexpr ,则此构造函数为 constexpr 。对于空元组 std:: tuple <> ,该构造函数为 constexpr 。
- 对所有 i , std:: is_move_constructible < Ti > :: value 必须为 true ,否则 行为未定义 (C++20 前) 此重载不参与重载决议 (C++20 起) 。
| 目录 | 
参数
| args | - | 用于初始化元组各元素的值 | 
| other | - | 用于初始化元组各元素的值构成的元组 | 
| p | - | 用于初始化二元组两个元素的数值对 | 
| 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)}); // 正确 launch_rocket_at({1, 2, 3}); // 错误:int 不能隐式转换为 duration 类型 launch_rocket_at(std::tuple<hours, minutes, seconds>{1, 2, 3}); // 正确
示例
#include <iomanip> #include <iostream> #include <memory> #include <string> #include <string_view> #include <tuple> #include <type_traits> #include <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个整数 using my_alloc = std::allocator<int>; std::vector<int, my_alloc> v{5, 1, my_alloc{/* 1 */}}; // 使用 my_alloc(2) 在元组中的vector内分配5个整数 std::tuple<int,
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 2510 | C++11 | 默认构造函数为隐式声明 | 改为条件性显式 | 
| LWG 3121 | C++11 | 单元素元组构造函数可能递归检查约束; 
          allocator_arg_t
         参数引发歧义 | 进一步约束 构造函数 | 
| LWG 3158 | C++11 | 对应默认构造函数的 分配器使用构造函数为隐式声明 | 改为条件性显式 | 
| LWG 3211 | C++11 | 
          tuple<>
         的默认构造函数是否为平凡类型未作规定 | 要求必须为平凡类型 | 
| LWG 4045 | C++23 | 
          
           tuple-like
          
         构造函数可能产生悬垂引用 | 定义为已删除 | 
| N4387 | C++11 | 部分构造函数为显式声明,阻碍了有用行为 | 多数构造函数改为 条件性显式 | 
参见
| 将一个 
          tuple
         的内容赋值给另一个(公开成员函数) | |
| 
           
            
             
              (C++11)
             
            
           
           | 根据参数类型创建对应类型的 
          tuple
         对象(函数模板) | 
| 
           
            
             
              (C++11)
             
            
           
           | 创建左值引用组成的
         
          
           tuple
          
         
         或将 tuple 解包为独立对象 (函数模板) | 
| 
           
            
             
              (C++11)
             
            
           
           | 创建由
         
          转发引用
         
         构成的 
          tuple
         (函数模板) | 
| 构造新的 
          pair
         ( 
           std::pair<T1,T2>
          的公开成员函数) |