名稱空間
變體
操作

std::forward_as_tuple

來自 cppreference.com
< cpp‎ | utility‎ | tuple
 
 
 
 
定義於標頭檔案 <tuple>
template< class... Types >
std::tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept;
(C++11 起)
(C++14 起為 constexpr)

構造一個引用元組,元組中的引用指向 args 中的引數,適合作為函式引數轉發。當實參是右值時,元組具有右值引用資料成員;否則具有左值引用資料成員。

目錄

[編輯] 引數

args - 零個或多個用於構造元組的引數

[編輯] 返回值

一個 std::tuple 物件,其建立方式如同 std::tuple<Types&&...>(std::forward<Types>(args)...)

[編輯] 注意

如果引數是臨時物件,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

[編輯] 參閱

建立一個由引數型別定義的 tuple 物件
(函式模板) [編輯]
(C++11)
建立左值引用 tuple 或將 tuple 解包為單獨的物件
(函式模板) [編輯]
(C++11)
透過連線任意數量的 tuple 建立一個 tuple
(函式模板) [編輯]
(C++17)
使用引數元組呼叫函式
(函式模板) [編輯]