構造
構造函數
tuple的構造函數很普通,沒啥說的。
default (1) | constexpr tuple();默認構造函數 |
---|---|
copy / move (2) | tuple (const tuple& tpl) = default; 拷貝構造函數 tuple (tuple&& tpl) = default;移動構造函數 |
implicit conversion (3) | template <class... UTypes> tuple (const tuple<UTypes...>& tpl); 廣義拷貝構造函數,只要對應參數可以默認轉換 template <class... UTypes> tuple (tuple<UTypes...>&& tpl); 廣義移動構造函數,只要對應參數可以默認轉換 |
initialization (4) | explicit tuple (const Types&... elems);構造並初始化 template <class... UTypes> explicit tuple (UTypes&&... elems);構造並初始化,參數類型只要求可以默認轉換 |
conversion from pair (5) | template <class U1, class U2> tuple (const pair<U1,U2>& pr); 從pair構造 template <class U1, class U2> tuple (pair<U1,U2>&& pr); |
allocator (6)
|
在以上5類的基礎上增加分配器參數,第一個參數無實際意義,只用來與(3)進行區別。 template<class Alloc> tuple (allocator_arg_t aa, const Alloc& alloc); template<class Alloc> tuple (allocator_arg_t aa, const Alloc& alloc, const tuple& tpl); template<class Alloc> tuple (allocator_arg_t aa, const Alloc& alloc, tuple&& tpl); template<class Alloc,class... UTypes> tuple (allocator_arg_t aa, const Alloc& alloc, const tuple<UTypes...>& tpl); template<class Alloc, class... UTypes> tuple (allocator_arg_t aa, const Alloc& alloc, tuple<UTypes...>&& tpl); template<class Alloc> tuple (allocator_arg_t aa, const Alloc& alloc, const Types&... elems); template<class Alloc, class... UTypes> tuple (allocator_arg_t aa, const Alloc& alloc, UTypes&&... elems); template<class Alloc, class U1, class U2> tuple (allocator_arg_t aa, const Alloc& alloc, const pair<U1,U2>& pr); template<class Alloc, class U1, class U2> tuple (allocator_arg_t aa, const Alloc& alloc, pair<U1,U2>&& pr); |
獲取tuple的分量
get模板函數可以獲取tuple分量的引用,如下圖所聲明的,常量tuple獲得常量引用,右值引用tuple獲得右值引用,非常量非右值引用獲得引用。
(1) | template <size_t I, class... Types> typename tuple_element< I, tuple<Types...> >::type& get(tuple<Types...>& tpl) noexcept; |
---|---|
(2) | template <size_t I, class... Types> typename tuple_element< I, tuple<Types...> >::type&& get(tuple<Types...>&& tpl) noexcept; |
(3) | template <size_t I, class... Types> typename tuple_element< I, tuple<Types...> >::type const& get(const tuple<Types...>& tpl) noexcept;
|
std::make_tuple
template<class... Types> tuple<VTypes...> make_tuple (Types&&... args);
make_tuple模板函數,根據實參類型生成一個tuple,並用實參的值對其進行初始化。編譯器在推斷tuple各分量的類型時會去掉實參的頂級const屬性、引用屬性(包括右值引用),也就是說造出來的tuple存的是值。數組會推斷為指針、函數會推斷為函數指針。如果需要推斷出引用類型,要借助std::ref或std::cref。
std::tie
template<class... Types> constexpr tuple<Types&...> tie (Types&... args) noexcept;
tie生成一個tuple,此tuple包含的分量全部為實參的引用,與make_tuple完全相反。主要用於從tuple中提取數據。例如:
int a,b,c;
auto x = make_tuple(1,2,3);
std::tie(a,b,c) = x;
std::forward_as_tuple
template<class... Types>
constexpr tuple<Types&&...> forward_as_tuple(Types&&...)noexcept;
同std::tie一樣,也是生成一個全是引用的tuple,不過std::tie只接受左值,而std::forward_as_tuple左值、右值都接受。主要是用於不損失類型屬性的轉發數據。
std::tuple_cat
template<class... Tuples>
tuple<CTypes...> tuple_cat(Tuples&&... tlps);
此函數接受多個tuple作為參數,然后返回一個tuple。返回的這個tuple將tuple_cat的參數中的tuple的所有元素按所屬的tuple在參數中的順序以及其在tuple中的順序排列成一個新的tuple。新tuple中元素的類型與參數中的tuple中的元素的類型完全一致。
template<class... Types>
struct tuple_size<tuple<Types...>>;
tuple_size為輔助類,用於獲取tuple中元素的個數。用法為:tuple_size<decltype(tuple)>::value
template<size_t I,class... Types>
struct tuple_element<I,tuple<Types ...>>;
tuple_element為輔助類,用於獲取tuple中某個元素的類型。用法為:tuple_size<1,decltype(tuple)>::type