std::make_tuple
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <tuple> で定義
|
||
template< class... Types > tuple<VTypes...> make_tuple( Types&&... args ); |
(C++11以上) (C++14以上ではconstexpr) |
|
引数の型から目的の型を推定してタプルオブジェクトを作成します。
Types... の各 Ti に対して、 VTypes... の対応する型 Vi は std::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)...);
}
|
例
Run this code
#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 を作成したり、タプルを個々のオブジェクトに分解したりします (関数テンプレート) | |
転送参照の tuple を作成します (関数テンプレート) | |
任意の数のタプルを連結して新たな tuple を作成します (関数テンプレート) | |
(C++17) |
タプルを引数として使用して関数を呼びます (関数テンプレート) |