首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::forward_as_tuple

Defined in header <tuple>

template< class... Types > tuple<Types&&...> forward_as_tuple( Types&&... args );

(since C++11) (until C++14)

template< class... Types > constexpr tuple<Types&&...> forward_as_tuple( Types&&... args );

(since C++14)

中的参数的引用的元组。args适合于作为参数转发给函数。当rvalue用作参数时,元组具有rvalue引用数据成员,否则具有lvalue引用数据成员。

参数

args

-

zero or more arguments to construct the tuple from

返回值

std::tuple对象创建的std::tuple<Types&&...>(std::forward<Types>(args)...)...

例外

noexcept规格:

noexcept

注记

如果争论是暂时的,forward_as_tuple不能延长它们的生命周期;它们必须在完整表达式结束前使用。

二次

代码语言:javascript
复制
#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);
}

二次

产出:

二次

代码语言:javascript
复制
m[10] = aaaaaaaaaaaaaaaaaaaa

二次

另见

make_tuple

creates a tuple object of the type defined by the argument types (function template)

tie

creates a tuple of lvalue references or unpacks a tuple into individual objects (function template)

tuple_cat

creates a tuple by concatenating any number of tuples (function template)

apply (C++17)

calls a function with a tuple of arguments (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券