std::forward_as_tuple
提供: cppreference.com
ヘッダ <tuple> で定義
|
||
template< class... Types > tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept; |
(C++11以上) (C++14以上ではconstexpr) |
|
関数に引数として転送するのに適した args
内の引数を指す参照のタプルを構築します。 タプルは引数として右辺値が使用される場合は右辺値参照のデータメンバを持ち、そうでなければ左辺値参照のデータメンバを持ちます。
目次 |
[編集] 引数
args | - | タプルを構築するための0個以上の引数 |
[編集] 戻り値
std::tuple<Types&&...>(std::forward<Types>(args)...) によって作成されたかのような std::tuple オブジェクト。
[編集] ノート
引数が一時オブジェクトの場合、 forward_as_tuple
はその生存期間を延長しません。 それらは完全式の終了前に使用されなければなりません。
[編集] 例
Run this code
#include <iostream> #include <map> #include <tuple> #include <string> int main() { std::map<int, std::string> m; m.emplace(std::piecewise_construct, std::forward_as_tuple(10), std::forward_as_tuple(20, 'a')); std::cout << "m[10] = " << m[10] << '\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[10] = aaaaaaaaaaaaaaaaaaaa
[編集] 関連項目
引数の型によって定義される型の tuple オブジェクトを作成します (関数テンプレート) | |
左辺値参照の tuple を作成したり、タプルを個々のオブジェクトに分解したりします (関数テンプレート) | |
任意の数のタプルを連結して新たな tuple を作成します (関数テンプレート) | |
(C++17) |
タプルを引数として使用して関数を呼びます (関数テンプレート) |