std::make_tuple
| (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を個々のオブジェクトに展開する
(関数テンプレート) |
|
(C++11)
|
転送参照
の
tuple
を作成する
(関数テンプレート) |
|
(C++11)
|
任意の数のtupleを連結して
tuple
を作成する
(関数テンプレート) |
|
(C++17)
|
引数のtupleで関数を呼び出す
(関数テンプレート) |