名前空間
変種
操作

std::make_tuple

From cppreference.com
< cpp‎ | utility‎ | tuple
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
ヘッダ <tuple> で定義
template< class... Types >
std::tuple<VTypes...> make_tuple( Types&&... args );
(C++11以降)
(C++14以降constexpr)

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

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

目次

[編集] パラメータ

args - タプルの構築に使用する、ゼロ個以上の引数。

[編集] 戻り値

指定された値を含む 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;
// or use std::unwrap_ref_decay_t (since C++20)
 
template <class... Types>
constexpr // since 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() // this function returns multiple values
{
    int x = 5;
    return std::make_tuple(x, 7); // return {x,7}; in C++17
}
 
int main()
{
    // heterogeneous tuple construction
    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";
 
    // function returning multiple values
    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 を生成する
(関数テンプレート) [編集]
(C++11)
任意の数のタプルを連結して tuple を生成する
(関数テンプレート) [編集]
(C++17)
引数のタプルを使って関数を呼び出す
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)