命名空间
变体
操作

标准库头文件 <tuple> (C++11)

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用工具
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

此头文件是 通用实用工具 库的一部分。

内容

包含

(C++20)
三路比较运算符 支持[edit]

(C++11)
实现固定大小的容器,它保存可能不同类型的元素
(类模板) [edit]
获取类似元组类型的元素数量
(类模板) [edit]
获取类似元组类型的元素类型
(类模板) [edit]
获取大小

一个tuple
(类模板特化) [编辑]

获取指定元素的类型
(类模板特化) [编辑]
特化 std::uses_allocator 类型特征
(类模板特化) [编辑]

常量

(C++11)
使用 tie 解包 tuple 时跳过元素的占位符
(常量) [编辑]

函数

创建由参数类型定义的类型的 tuple 对象
(函数模板) [编辑]
(C++11)
创建 tuple 的左值引用或将元组解包到单个对象
(函数模板) [编辑]
创建 转发引用tuple
(函数模板) [编辑]
(C++11)
通过连接任意数量的元组来创建一个 tuple
(函数模板) [编辑]
元组访问指定元素
(函数模板) [编辑]
(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20)
按字典顺序比较元组中的值
(函数模板) [编辑]
特化 std::swap 算法
(函数模板) [编辑]
(C++17)
使用元组参数调用函数
(函数模板) [编辑]
使用元组参数构造对象
(函数模板) [编辑]

[编辑] 概述

#include <compare>
 
namespace std {
  // class template tuple
  template<class... Types>
    class tuple;
 
  // tuple creation functions
  inline constexpr /* unspecified */ ignore;
 
  template<class... TTypes>
    constexpr tuple<unwrap_ref_decay_t<TTypes>...> make_tuple(TTypes&&...);
 
  template<class... TTypes>
    constexpr tuple<TTypes&&...> forward_as_tuple(TTypes&&...) noexcept;
 
  template<class... TTypes>
    constexpr tuple<TTypes&...> tie(TTypes&...) noexcept;
 
  template<class... Tuples>
    constexpr tuple<CTypes...> tuple_cat(Tuples&&...);
 
  // calling a function with a tuple of arguments
  template<class F, class Tuple>
    constexpr decltype(auto) apply(F&& f, Tuple&& t);
 
  template<class T, class Tuple>
    constexpr T make_from_tuple(Tuple&& t);
 
  // tuple helper classes
  template<class T> struct tuple_size;                  // not defined
  template<class T> struct tuple_size<const T>;
 
  template<class... Types> struct tuple_size<tuple<Types...>>;
 
  template<size_t I, class T> struct tuple_element;     // not defined
  template<size_t I, class T> struct tuple_element<I, const T>;
 
  template<size_t I, class... Types>
    struct tuple_element<I, tuple<Types...>>;
 
  template<size_t I, class T>
    using tuple_element_t = typename tuple_element<I, T>::type;
 
  // element access
  template<size_t I, class... Types>
    constexpr tuple_element_t<I, tuple<Types...>>& get(tuple<Types...>&) noexcept;
  template<size_t I, class... Types>
    constexpr tuple_element_t<I, tuple<Types...>>&& get(tuple<Types...>&&) noexcept;
  template<size_t I, class... Types>
    constexpr const tuple_element_t<I, tuple<Types...>>&
      get(const tuple<Types...>&) noexcept;
  template<size_t I, class... Types>
    constexpr const tuple_element_t<I, tuple<Types...>>&&
      get(const tuple<Types...>&&) noexcept;
  template<class T, class... Types>
    constexpr T& get(tuple<Types...>& t) noexcept;
  template<class T, class... Types>
    constexpr T&& get(tuple<Types...>&& t) noexcept;
  template<class T, class... Types>
    constexpr const T& get(const tuple<Types...>& t) noexcept;
  template<class T, class... Types>
    constexpr const T&& get(const tuple<Types...>&& t) noexcept;
 
  // relational operators
  template<class... TTypes, class... UTypes>
    constexpr bool operator==(const tuple<TTypes...>&, const tuple<UTypes...>&);
  template<class... TTypes, class... UTypes>
    constexpr common_comparison_category_t</*synth-three-way-result*/<TTypes, UTypes>...>
      operator<=>(const tuple<TTypes...>&, const tuple<UTypes...>&);
 
  // allocator-related traits
  template<class... Types, class Alloc>
    struct uses_allocator<tuple<Types...>, Alloc>;
 
  // specialized algorithms
  template<class... Types>
    constexpr void
      swap(tuple<Types...>& x, tuple<Types...>& y) noexcept(/* see description */);
 
  // tuple helper classes
  template<class T>
    inline constexpr size_t tuple_size_v = tuple_size<T>::value;
}
 
// deprecated
namespace std {
  template<class T> class tuple_size<volatile T>;
  template<class T> class tuple_size<const volatile T>;
 
  template<size_t I, class T> class tuple_element<I, volatile T>;
  template<size_t I, class T> class tuple_element<I, const volatile T>;
}

[编辑] 类模板 std::tuple

namespace std {
  template<class... Types>
  class tuple {
  public:
    // tuple construction
    constexpr explicit(/* see description */) tuple();
    constexpr explicit(/* see description */)
      tuple(const Types&...);         // only if sizeof...(Types) >= 1
    template<class... UTypes>
      constexpr explicit(/* see description */)
        tuple(UTypes&&...);           // only if sizeof...(Types) >= 1
 
    tuple(const tuple&) = default;
    tuple(tuple&&) = default;
 
    template<class... UTypes>
      constexpr explicit(/* see description */) tuple(const tuple<UTypes...>&);
    template<class... UTypes>
      constexpr explicit(/* see description */) tuple(tuple<UTypes...>&&);
 
    template<class U1, class U2>
      constexpr explicit(/* see description */)
        tuple(const pair<U1, U2>&);   // only if sizeof...(Types) == 2
    template<class U1, class U2>
      constexpr explicit(/* see description */)
        tuple(pair<U1, U2>&&);        // only if sizeof...(Types) == 2
 
    // allocator-extended constructors
    template<class Alloc>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a);
    template<class Alloc>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a, const Types&...);
    template<class Alloc, class... UTypes>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a, UTypes&&...);
    template<class Alloc>
      constexpr tuple(allocator_arg_t, const Alloc& a, const tuple&);
    template<class Alloc>
      constexpr tuple(allocator_arg_t, const Alloc& a, tuple&&);
    template<class Alloc, class... UTypes>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&);
    template<class Alloc, class... UTypes>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&&);
    template<class Alloc, class U1, class U2>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&);
    template<class Alloc, class U1, class U2>
      constexpr explicit(/* see description */)
        tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&&);
 
    // tuple assignment
    constexpr tuple& operator=(const tuple&);
    constexpr tuple& operator=(tuple&&) noexcept(/* see description */);
 
    template<class... UTypes>
      constexpr tuple& operator=(const tuple<UTypes...>&);
    template<class... UTypes>
      constexpr tuple& operator=(tuple<UTypes...>&&);
 
    template<class U1, class U2>
      constexpr tuple& operator=(const pair<U1, U2>&); // only if sizeof...(Types) == 2
    template<class U1, class U2>
      constexpr tuple& operator=(pair<U1, U2>&&);      // only if sizeof...(Types) == 2
 
    // tuple swap
    constexpr void swap(tuple&) noexcept(/* see description */);
  };
 
  template<class... UTypes>
    tuple(UTypes...) -> tuple<UTypes...>;
  template<class T1, class T2>
    tuple(pair<T1, T2>) -> tuple<T1, T2>;
  template<class Alloc, class... UTypes>
    tuple(allocator_arg_t, Alloc, UTypes...) -> tuple<UTypes...>;
  template<class Alloc, class T1, class T2>
    tuple(allocator_arg_t, Alloc, pair<T1, T2>) -> tuple<T1, T2>;
  template<class Alloc, class... UTypes>
    tuple(allocator_arg_t, Alloc, tuple<UTypes...>) -> tuple<UTypes...>;
}