名前空間
変種
操作

std::make_tuple

提供: cppreference.com
< cpp‎ | utility‎ | tuple
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
std::tuple
メンバ関数
非メンバ関数
make_tuple
(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20未満)(C++20)
推定ガイド(C++17)
ヘルパークラス
 
ヘッダ <tuple> で定義
template< class... Types >
tuple<VTypes...> make_tuple( Types&&... args );
(C++11以上)
(C++14以上ではconstexpr)

引数の型から目的の型を推定してタプルオブジェクトを作成します。

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

目次

[編集] 引数

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

[編集] 戻り値

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

[編集] 実装例

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 special_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type;
 
template <class... Types>
auto make_tuple(Types&&... args)
{
    return std::tuple<special_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

[編集] 関連項目

左辺値参照の tuple を作成したり、タプルを個々のオブジェクトに分解したりします
(関数テンプレート) [edit]
転送参照tuple を作成します
(関数テンプレート) [edit]
任意の数のタプルを連結して新たな tuple を作成します
(関数テンプレート) [edit]
(C++17)
タプルを引数として使用して関数を呼びます
(関数テンプレート) [edit]