Namespaces
Variants

std:: make_tuple

From cppreference.net
Utilities library
ヘッダーで定義 <tuple>
template < class ... Types >
std:: tuple < VTypes... > make_tuple ( Types && ... args ) ;
(C++11以降)
(C++14以降 constexpr)

引数の型から対象の型を推論して、タプルオブジェクトを作成します。

Types... 内の各 Ti について、対応する型 Vi VTypes... 内で std:: decay < Ti > :: type となります。ただし、 std::decay の適用結果が何らかの型 X に対する std:: reference_wrapper < X > となる場合、推定される型は X& となります。

目次

パラメータ

args - タプルを構築するための0個以上の引数

戻り値

指定された値を含む std::tuple オブジェクト。 std:: tuple < VTypes... > ( std:: forward < Types > ( t ) ... ) . によって作成されたかのように生成されます。

実装例

template <class T>
struct unwrap_refwrapper
{
    using type = T;
};
template <class T>
struct unwrap_refwrapper<std::reference_wrapper<T>>
{
    using type = T&;
};
template <class T>
using unwrap_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type;
// または std::unwrap_ref_decay_t を使用 (C++20以降)
template <class... Types>
constexpr // C++14以降
std::tuple<unwrap_decay_t<Types>...> make_tuple(Types&&... args)
{
    return std::tuple<unwrap_decay_t<Types>...>(std::forward<Types>(args)...);
}

#include <iostream>
#include <tuple>
#include <functional>
std::tuple<int, int> f() // この関数は複数の値を返す
{
    int x = 5;
    return std::make_tuple(x, 7); // C++17では return {x,7};
}
int main()
{
    // 異種混合のtuple構築
    int n = 1;
    auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
    n = 7;
    std::cout << "The value of t is ("
              << std::get<0>(t) << ", "
              << std::get<1>(t) << ", "
              << std::get<2>(t) << ", "
              << std::get<3>(t) << ", "
              << std::get<4>(t) << ")\n";
    // 複数の値を返す関数
    int a, b;
    std::tie(a, b) = f();
    std::cout << a << ' ' << b << '\n';
}

出力:

The value of t is (10, Test, 3.14, 7, 1)
5 7

関連項目

(C++11)
左辺値参照の tuple を作成する、またはtupleを個々のオブジェクトに展開する
(関数テンプレート)
転送参照 tuple を作成する
(関数テンプレート)
(C++11)
任意の数のtupleを連結して tuple を作成する
(関数テンプレート)
(C++17)
引数のtupleで関数を呼び出す
(関数テンプレート)