简体   繁体   English

将参数包传递给另一个可变参数模板函数时,是否必须使用std :: forward

[英]Do I have to use std::forward when passing arguments pack to another variadic template function

How do I implement a function (foo) which passes a parameter pack to a function (bar) which finally passes that paramer pack to a class constructor? 如何实现将参数包传递给函数(栏)的函数(foo),该函数最终将参数包传递给类构造函数? This is the code: 这是代码:

template <typename... Args>
void bar(Args&&... args)
{
    MyObj obj(std::forward<Args>(args)...);
    /* Do something with obj */
}

Do I have to implement foo as 我是否必须将foo实现为

template <typename... Args>
void foo(Args&&... args)
{ bar(args...); }

Or 要么

template <typename... Args>
void foo(Args&&... args)
{ bar(std::forward<Args>(args)...); }

You need to use forward . 您需要使用forward forward looks like this: forward看起来像这样:

template <class T>
constexpr T&& forward(typename std::remove_reference<T>::type& t) noexcept
{
    return static_cast<T&&>(t);
}

(There is another overload of forward , but it is irrelevant here.) forward另一个重载,但在这里是无关紧要的。)

For each element Arg of the template parameter pack Args , Arg can be either: ( T is a non-reference type) 对于模板参数包Args每个元素ArgArg可以是:( T是非引用类型)

  • T& if the argument is a non-const lvalue, so that the parameter is of type T& ; T&如果参数是非常量左值,则参数为T&类型; or 要么

  • const T& if the argument is a const lvalue, so that the parameter is of type const T& ; const T&如果参数是const左值),则参数为const T&类型; or 要么

  • T if the argument is an rvalue, so that the parameter is of type T&& . T如果参数是一个rvalue,使得参数的类型的T&&

In the first case, forward<Arg>(arg) instantiates to: 在第一种情况下, forward<Arg>(arg)实例化为:

constexpr T& forward(T& t) noexcept
{
    return static_cast<T&>(t);
}

resulting in a non-const lvalue to be passed. 导致传递非常量左值。

In the second case, forward<Arg>(arg) instantiates to: 在第二种情况下, forward<Arg>(arg)实例化为:

constexpr const T& forward(const T& t) noexcept
{
    return static_cast<const T&>(t);
}

resulting in a const lvalue to be passed. 导致要传递一个常量左值。

In the last case, forward<Arg>(arg) instantiates to: 在最后一种情况下, forward<Arg>(arg)实例化为:

constexpr T&& forward(T&& t) noexcept
{
    return static_cast<T&&>(t);
}

resulting in an rvalue to be passed. 导致传递右值。

In all three cases, the value category is preserved, and the value is forwarded with modification to bar . 在所有这三种情况下,值类别都会保留,并且值会修改为bar而转发。

If you don't use forward , you will be unconditionally passing an lvalue, which is not desired. 如果不使用forward ,则将无条件传递一个左值,这是不希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递std :: array作为模板可变参数的参数 - Passing std::array as arguments of template variadic function 将可变参数模板参数传递给另一个函数 - Passing variadic function template arguments to another function 将可变参数模板 function arguments 传递给另一个 function - Passing variadic template function arguments to another function 将可变参数模板参数传递给可变参数函数 - Passing variadic template arguments to a variadic function 将可变参数模板参数包传递给下一个函数 - Passing variadic template argument pack to next function 将参数传递给另一个可变参数函数 - Passing arguments to another variadic function 提取可变参数模板参数包并在类型特征元函数中的另一个可变参数模板中使用它? - Extract variadic template parameter pack and use it in another variadic template in a type traits meta-function? 没有与可变参数调用std :: forward(const std :: string&)的匹配函数 - No matching function for call std::forward(const std::string &) with variadic arguments 将可变参数函数参数转发给另一个可变参数函数而无需成本 - Forward variadic function arguments to another variadic function without cost std :: function到可变参数成员函数,然后绑定可变参数模板参数 - std::function to variadic member function and then bind variadic template arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM