std::forward_as_tuple
From cppreference.com
| ヘッダ <tuple> で定義 |
||
template< class... Types > std::tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept; |
(C++11以降) (C++14以降constexpr) |
|
args の引数から、関数への引数として転送するのに適した参照のタプルを構築します。タプルは、引数として Rvalue が使用される場合は Rvalue 参照データメンバーを、そうでない場合は Lvalue 参照データメンバーを持ちます。
目次 |
[編集] パラメータ
| args | - | タプルを構築するためのゼロ個以上の引数 |
[編集] 戻り値
std::tuple<Types&&...>( std::forward<Types>(args) )... として作成された std::tuple オブジェクト。
[編集] ノート
引数が一時オブジェクトである場合、forward_as_tuple はそれらの生存期間を延長しません。それらは完全な式が終わる前に使用される必要があります。
[編集] 例
このコードを実行
#include <iostream> #include <map> #include <string> #include <tuple> int main() { std::map<int, std::string> m; m.emplace(std::piecewise_construct, std::forward_as_tuple(6), std::forward_as_tuple(9, 'g')); std::cout << "m[6] = " << m[6] << '\n'; // The following is an error: it produces a // std::tuple<int&&, char&&> holding two dangling references. // // auto t = std::forward_as_tuple(20, 'a'); // m.emplace(std::piecewise_construct, std::forward_as_tuple(10), t); }
出力
m[6] = ggggggggg
[編集] 関連項目
| (C++11) |
引数型によって定義された型の tuple オブジェクトを生成する(関数テンプレート) |
| (C++11) |
左辺値参照のtupleを生成するか、タプルを個別のオブジェクトにアンパックする (関数テンプレート) |
| (C++11) |
任意の数のタプルを連結して tuple を生成する(関数テンプレート) |
| (C++17) |
引数のタプルを使って関数を呼び出す (関数テンプレート) |