std::forward_as_tuple
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <tuple>
|
||
| template< class... Types > tuple<Types...> forward_as_tuple( Types&&... args ); |
(C++11 起) | |
構造一個元組中的參數
args適合作為函數的參數進行轉發。該元組的右值引用的數據成員時,右值作為參數,否則有左值引用的數據成員。如果右值使用此功能前必須消耗的下一個序列點,結果.Original:
Constructs a tuple of references to the arguments in
args suitable for forwarding as an argument to a function. The tuple has rvalue reference data members when rvalues are used as arguments, and otherwise has lvalue reference data members. If rvalues are used, the result of this function must be consumed before the next sequence point.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目錄 |
[编辑] 參數
| args | - | 零個或多個參數來構造的元組
Original: zero or more arguments to construct the tuple from The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 返回值
一個
std::tuple創建的對象,如果std::tuple<Types&&...>(std::forward<Types>(args)...)Original:
A
std::tuple object created as if by std::tuple<Types&&...>(std::forward<Types>(args)...)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 例外
[编辑] 為例
#include <iostream> #include <map> #include <tuple> #include <string> int main() { std::map<int, std::string> m; // same as m.emplace(10, 20, 'a'); m.emplace(std::forward_as_tuple(10, std::string(20, 'a'))); std::cout << "m[10] = " << m[10] << '\n'; // The following is an error: it produces a // std::tuple<int&&, std::string&&> holding two dangling references. // // auto t = std::forward_as_tuple(10, std::string(20, 'a')); // m.emplace(t); }
Output:
m[10] = aaaaaaaaaaaaaaaaaaaa
| 創建一個 tuple對象的參數類型定義的類型Original: creates a tuple object of the type defined by the argument typesThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函數模板) | |
| 創建一個 tuple的左值引用或解壓縮到單個對象的元組Original: creates a tuple of lvalue references or unpacks a tuple into individual objectsThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函數模板) | |
| 創建一個 tuple通過連接任意數量的元組Original: creates a tuple by concatenating any number of tuplesThe text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函數模板) | |