Namespaces
Variants

std::make_tuple

ja.cppreference.net より
 
 
外部ライブラリ
ユーティリティライブラリ
言語サポート型サポート
(基本型、RTTI) ライブラリ機能テストマクロ
(C++20)
プログラムユーティリティ
initializer_list
is_constant_evaluated
is_within_lifetime
source_location
(C++20) コルーチンサポート
(C++20) 契約サポート
(C++26)
three_way_comparable_with(C++20)
strong_ordering
weak_ordering
partial_ordering
common_comparison_category
compare_three_way_result
compare_three_way
strong_order
weak_order
partial_order
compare_strong_order_fallback
compare_weak_order_fallback
compare_partial_order_fallback    
is_lteq(C++20)(C++20)    
is_gteq(C++20)(C++20)

(C++20)
(C++11) 関係演算子
=
cmp_less_than(C++20)(C++20)    
cmp_greater_than(C++20)(C++20)
in_range
(C++20)swap と
ranges::swap
exchange
declval
to_underlying
forward
forward_like
move_if_noexcept
as_const
(C++17)
tuple
optional
variant
tuple_size
tuple_element
apply
make_from_tuple
expected



 
 
(C++17)<tuple>
template< class... Types >
std::tuple<VTypes...> make_tuple( Types&&... args );
ヘッダ
で定義 (C++11以上)

(C++14以降constexpr)

引数の型からターゲット型を推定してタプルオブジェクトを作成します。 Ti各 Types... について、Vi 内の対応する型 VTypes... は、std::decay<Ti>::typestd::decay を適用した結果が何らかの型 に対して std::reference_wrapper<X> となる場合を除き 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で関数を呼び出す
(関数テンプレート)