「cpp/utility/tuple/forward as tuple」の版間の差分
提供: cppreference.com
細 (1版:Translate from the English version) |
|||
(2人の利用者による、間の3版が非表示) | |||
1行: | 1行: | ||
− | |||
{{cpp/title|forward_as_tuple}} | {{cpp/title|forward_as_tuple}} | ||
{{cpp/utility/tuple/navbar}} | {{cpp/utility/tuple/navbar}} | ||
− | {{ | + | {{begin}} |
− | {{ | + | {{header | tuple}} |
− | {{ | + | {{| notes={{mark since c++}} | 1= |
template< class... Types > | template< class... Types > | ||
− | tuple<Types...> forward_as_tuple( Types&&... args ); | + | tuple<Types...> forward_as_tuple( Types&&... args ) ; |
}} | }} | ||
− | {{ | + | {{end}} |
− | + | {{tt|args}} | |
− | === | + | ====== |
− | {{ | + | {{begin}} |
− | {{ | + | {{| args | }} |
− | {{ | + | {{end}} |
− | === | + | ====== |
− | + | {{c|std::tuple<Types&&...>(std::forward<Types>(args)...)}} {{|std::tuple}} | |
− | === | + | ====== |
− | {{ | + | {{}} |
===例=== | ===例=== | ||
35行: | 34行: | ||
std::map<int, std::string> m; | std::map<int, std::string> m; | ||
− | + | m.emplace(, | |
− | + | std::forward_as_tuple(10, | |
+ | std::(20, 'a')); | ||
std::cout << "m[10] = " << m[10] << '\n'; | std::cout << "m[10] = " << m[10] << '\n'; | ||
// The following is an error: it produces a | // The following is an error: it produces a | ||
− | // std::tuple<int&&, | + | // std::tuple<int&&, &&> holding two dangling references. |
// | // | ||
− | // auto t = std::forward_as_tuple | + | // auto t = std::forward_as_tuple(20, 'a'); |
− | // m.emplace(t); | + | // m.emplace(t); |
} | } | ||
| output= | | output= | ||
49行: | 49行: | ||
}} | }} | ||
− | {{ | + | |
− | {{ | + | {{begin}} |
− | {{ | + | {{| cpp/utility/tuple/make_tuple}} |
− | {{ | + | {{| cpp/utility/tuple/tie}} |
− | {{ | + | {{| cpp/utility/tuple/tuple_cat}} |
+ | {{ | ||
+ | end}} | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ |
2018年9月5日 (水) 05:25時点における最新版
ヘッダ <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) |
タプルを引数として使用して関数を呼びます (関数テンプレート) |