?
定義于頭文件 <tuple>
template< class... Types > | (C++11 起) |
類模板 std::tuple
是固定大小的異類值匯集。它是 std::pair 的推廣。
若 (std::is_trivially_destructible_v<Types> && ...) 為 true ,則 | (C++17 起) |
模板形參
Types... | - | tuple 所存儲的元素的類型。支持空列表。 |
?
構造函數
std::tuple<Types...>::tuple
constexpr tuple(); | (1) | (C++11 起) (條件性 explicit ) |
tuple( const Types&... args ); | (2) | (C++11 起) (C++14 起為 constexpr )(條件性 explicit ) |
template< class... UTypes > | (3) | (C++11 起) (C++14 起為 constexpr )(條件性 explicit ) |
template< class... UTypes > | (4) | (C++11 起) (C++14 起為 constexpr )(條件性 explicit ) |
template <class... UTypes> | (5) | (C++11 起) (C++14 起為 constexpr )(條件性 explicit ) |
template< class U1, class U2 > | (6) | (C++11 起) (C++14 起為 constexpr )(條件性 explicit ) |
template< class U1, class U2 > | (7) | (C++11 起) (C++14 起為 constexpr )(條件性 explicit ) |
tuple( const tuple& other ) = default; | (8) | (C++11 起) |
tuple( tuple&& other ) = default; | (9) | (C++11 起) |
template< class Alloc > | (10) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?const Types&... args ); | (11) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?UTypes&&... args ); | (12) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template <class Alloc, class... UTypes> tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?const tuple<UTypes...>& other ); | (13) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?tuple<UTypes...>&& other ); | (14) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?const pair<U1, U2>& p ); | (15) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?pair<U1, U2>&& p ); | (16) | (C++11 起) (C++20 起為 constexpr )(條件性 explicit ) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?const tuple& other ); | (17) | (C++11 起) (C++20 起為 constexpr ) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, ? ? ? ?tuple&& other ); | (18) | (C++11 起) (C++20 起為 constexpr ) |
?構造新的 tuple
。
1) 默認構造函數。值初始化所有元素。
此重載僅若 std::is_default_constructible<Ti>::value 對所有 i
為 true 才參與重載決議。
構造函數若且唯若對至少一個 i
有 Ti
不可從 {} 復制列表初始化才為 explicit 。
2) 直接構造函數。以對應參數初始化 tuple 的每個元素。
此重載僅若 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) 中的對應值初始化 tuple 的每個元素。
此重載僅若 sizeof...(Types) == sizeof...(UTypes) 且 sizeof...(Types) >= 1 與 std::is_constructible<Ti, Ui&&>::value 對所有 i
為 true 才參與重載決議。
構造函數若且唯若才 std::is_convertible<Ui&&, Ti>::value 對至少一個 i 為 false 為 explicit 。
4) 轉換復制構造函數。對 sizeof...(UTypes) 中所有 i
,以 std::get<i>(other) 初始化 tuple 的第 i 個元素。
此重載僅若
sizeof...(Types) == sizeof...(UTypes) 且
std::is_constructible_v<Ti, const Ui&> 對所有 i
為 true 且
sizeof...(Types) != 1 或
( Types...
展開成 T
且 UTypes...
展開成 U
時) std::is_convertible_v<const tuple<U>&, T> 、 std::is_constructible_v<T, const tuple<U>&> 與 std::is_same_v<T, U> 均為 false
才參與重載決議。
構造函數若且唯若 std::is_convertible<const Ui&, Ti>::value 對至少一個 i
為 false 才為 explicit 。
5) 轉換移動構造函數。對 sizeof...(UTypes) 中所有 i
,以 std::forward<Ui>(std::get<i>(other)) 初始化 tuple 的第 i 個元素。
此重載僅若
sizeof...(Types) == sizeof...(UTypes) 且
std::is_constructible_v<Ti, Ui&&> 對所有 i
為 true 且
sizeof...(Types) != 1 或
( Types...
展開成 T
且 UTypes...
展開成 U
時) std::is_convertible_v<tuple<U>, T> 、 std::is_constructible_v<T, tuple<U>> 與 std::is_same_v<T, U> 均為 false
才參與重載決議。
構造函數若且唯若 std::is_convertible<Ui&&, Ti>::value 對至少一個 i
為 false 才為 explicit 。
6) pair 復制構造函數。構造 2-tuple ,從 p.first
構造第一個元素,從 p.second
構造第二個元素。
此重載僅若 sizeof...(Types) == 2 與 std::is_constructible<T0,const U1&>::value 與 std::is_constructible<T1, const U2&>::value 均為 true 才參與重載決議。
構造函數若且唯若 std::is_convertible<const U1&, T0>::value 或 std::is_convertible<const U2&, T1>::value 為 false 才為 explicit 。 }}
7) pair 移動構造函數。構造 2-tuple ,從 std::forward<U1>(p.first) 構造第一個元素,從 std::forward<U2>(p.second) 構造第二個元素。
此重載僅若 sizeof...(Types) == 2 與 std::is_constructible<T0, U1&&>::value 與 std::is_constructible<T1, U2&&>::value 均為 true 才參與重載決議。
構造函數若且唯若 std::is_convertible<U1&&, T0>::value 或 std::convertible<U2&&, T1>::value 為 false 才為 explicit 。
8) 隱式定義的復制構造函數。以 other
的對應元素初始化 tuple 的每個元素。
若此函數進行的每個操作都是 constexpr ,則它是 constexpr 。對于空 tuple std::tuple<> ,它是 constexpr 。
要求 std::is_copy_constructible<Ti>::value 對所有 i
為 true 。
9) 隱式定義的移動構造函數。以 std::forward<Ui>(std::get<i>(other)) 構造 tuple 的第 i 個元素。
若此函數進行的每個操作都是 constexpr ,則它是 constexpr 。對于空 tuple std::tuple<> ,它是 constexpr 。
要求 std::is_move_constructible<Ti>::value 對所有 i
為 true 。
10-18) 等同于 (1-9) ,除了以使用分配器構造創建每個元素每個元素,即以分配器 (Allocator) 對象 a
為額外參數傳遞給每個 std::uses_allocator<Ui, Alloc>::value 為 true 的對象的構造函數。
參數
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 <tuple>
#include <iostream>
#include <string>
#include <typeinfo>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <memory>// 打印任何大小 tuple 的輔助函數
template<class Tuple, std::size_t N>
struct TuplePrinter
{static void print(const Tuple& t){TuplePrinter < Tuple, N - 1 >::print(t);std::cout << ", " << std::get < N - 1 > (t);}
};template<class Tuple>
struct TuplePrinter<Tuple, 1>
{static void print(const Tuple& t){std::cout << 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 << ")" << std::endl;
}
// 輔助函數結束int main()
{//1) 默認構造函數。值初始化所有元素。std::tuple<int, std::string, double> tuple1;std::cout << "Value-initialized: ";print(tuple1);//2) 直接構造函數。以對應參數初始化 tuple 的每個元素。std::tuple<int, std::string, double> tuple2(42, "Test", -3.14);std::cout << "Initialized with values: ";print(tuple2);//3) 轉換構造函數。以 std::forward<UTypes>(args) 中的對應值初始化 tuple 的每個元素。std::tuple<char, std::string, int> tuple3(std::move(tuple2));std::cout << "Implicitly converted: ";print(tuple3);//4) 轉換復制構造函數。對 sizeof...(UTypes) 中所有 i ,以 std::get<i>(other) 初始化 tuple 的第 i 個元素。std::tuple<char, std::string, int> tuple4(tuple2);std::cout << "Implicitly converted: ";print(tuple4);//5) 轉換移動構造函數。對 sizeof...(UTypes) 中所有 i ,//以 std::forward<Ui>(std::get<i>(other)) 初始化 tuple 的第 i 個元素。std::tuple<char, std::string, int> tuple5(std::move(tuple2));std::cout << "Implicitly converted: ";print(tuple5);//6) pair 復制構造函數。構造 2-tuple ,從 p.first 構造第一個元素,從 p.second 構造第二個元素。std::tuple<int, double> tuple6(std::make_pair(42, 3.14));std::cout << "Constructed from a pair";print(tuple6);//7) pair 移動構造函數。構造 2-tuple ,從 std::forward<U1>(p.first) 構造第一個元素,//從 std::forward<U2>(p.second) 構造第二個元素。std::pair<int, double> pair1 = std::make_pair(42, 3.14);std::tuple<int, double> tuple7(std::move(pair1));std::cout << "Constructed from a pair";print(tuple7);//8) 隱式定義的復制構造函數。以 other 的對應元素初始化 tuple 的每個元素。std::tuple<char, std::string, int> tuple8(tuple2);std::cout << "Implicitly converted: ";print(tuple8);//9) 隱式定義的移動構造函數。以 std::forward<Ui>(std::get<i>(other)) 構造 tuple 的第 i 個元素。std::tuple<char, std::string, int> tuple9(std::move(tuple2));std::cout << "Implicitly converted: ";print(tuple9);return 0;
}
輸出
Value-initialized: (0, , 0)
Initialized with values: (42, Test, -3.14)
Implicitly converted: (*, Test, -3)
Implicitly converted: (*, , -3)
Implicitly converted: (*, , -3)
Constructed from a pair(42, 3.14)
Constructed from a pair(42, 3.14)
Implicitly converted: (*, , -3)
Implicitly converted: (*, , -3)