名稱空間
變體
操作

std::tuple_cat

來自 cppreference.com
< cpp‎ | utility‎ | tuple
 
 
 
 
定義於標頭檔案 <tuple>
template< class... Tuples >
std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
(C++11 起)
(until C++14)
template< class... Tuples >
constexpr std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
(C++14 起)
(直至 C++23)
template< tuple-like... Tuples >
constexpr std::tuple</* CTypes */...> tuple_cat( Tuples&&... args );
(C++23 起)

構造一個元組,它是 args 中所有元組的連線。返回元組的元素型別 /* CTypes */ 透過按順序連線 Tuples 中所有 std::tuple(直到 C++23)tuple-like(自 C++23 起) 型別的元素型別包形成。

如果 std::decay_t<Tuples>... 中的任何型別不是 std::tuple 的特化,則行為是未定義的。然而,實現可以選擇支援遵循 tuple-like 協議的型別(例如 std::arraystd::pair)。

(直至 C++23)

型別 std::decay_t<Tuples>... 被限制為 tuple-like,即其中的每個型別都必須是 std::tuple 的特化或其他型別(例如 std::arraystd::pair),它模擬 tuple-like

(C++23 起)

如果 /* CTypes */ 中的任何型別不可從 args 中連線的元素序列中相應元素的型別構造,則 行為是未定義的(直到 C++23)程式格式錯誤(自 C++23 起)

目錄

[edit] 引數

args - 零個或多個要連線的元組

[edit] 返回值

一個 std::tuple 物件,由所有引數元組的所有元素組成,這些元素從每個單獨元素的 std::get<j>(std::forward<Ti>(arg)) 構造。

[edit] 示例

#include <iostream>
#include <string>
#include <tuple>
 
// helper function to print a tuple of any size
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<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
    std::cout << "()\n";
}
 
template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")\n";
}
// end helper function
 
int main()
{
    std::tuple<int, std::string, float> t1(10, "Test", 3.14);
    int n = 7;
    auto t2 = std::tuple_cat(t1, std::make_tuple("Foo", "bar"), t1, std::tie(n));
    n = 42;
    print(t2);
}

輸出

(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)

[edit] 參閱

建立一個由引數型別定義的 tuple 物件
(函式模板) [edit]
(C++11)
建立左值引用 tuple 或將 tuple 解包為單獨的物件
(函式模板) [edit]
建立一個轉發引用tuple
(函式模板) [edit]