std::forward_as_tuple
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <tuple>
  | 
||
|   template< class... Types > tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept;  | 
 (C++11 起)  (C++14 前)  | 
|
|   template< class... Types > constexpr tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept;  | 
(C++14 起) | |
构造到 args 中参数的,适于转发成函数参数的引用的 tuple 。该 tuple 在以右值为参数时拥有右值引用数据成员,否则拥有左值引用数据成员。
参数
| args | - | 构造 tuple 所用的零或更多参数 | 
返回值
如同以 std::tuple<Types&&...>(std::forward<Types>(args)...) 创建的 std::tuple 对象。
注意
若参数是临时量,则 forward_as_tuple 不延续其生存期;必须在完整表达式结尾前使用它们。
示例
运行此代码
#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'; // 下面是错误:它产生保有二个悬垂引用的 std::tuple<int&&, char&&> // // 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 解包为独立对象 (函数模板)  | |
   通过连接任意数量的元组来创建一个tuple (函数模板)  | |
|    (C++17)  | 
  以一个实参的元组来调用函数  (函数模板)  |