简体   繁体   English

使用 std::forward 进行参数包扩展的 '...' 的语法

[英]Syntax of '…' with Parameter Pack Expansion using std::forward

In C++, it seems that a parameter may be normally expanded with ... directly after the parameter pack's name.在 C++ 中,似乎可以在参数包名称之后直接使用...扩展参数。 For example,例如,

template <class... Tys>
void function(Tys... params) {
    function(params...);
}

However, when using std::forward , the ... goes seemingly after the parameter pack's name.但是,当使用std::forward时, ...似乎参数包的名称之后。 For example,例如,

template <class... Tys>
void function(Tys... params) {
    function(std::forward<Tys>(params)...); // The '...' is outside the parenthesis of 'params'
}

My question is why is there a difference?我的问题是为什么有区别? Why is the following code incorrect?为什么下面的代码不正确?

template<class... Tys>
void function(Tys... params) {
    function(std::forward<Tys>(params...));
}

At what point in the parameter pack expanded?参数包中的哪一点展开? I probably am not fully understanding how std::forward works or how it forwards arguments perfectly.我可能不完全理解std::forward是如何工作的,或者它如何完美地转发 arguments。

I appreciate any answers!我很感激任何答案!

The ... applies to the thing on its left. ...适用于它左边的东西。

  • function(params...)

    expands to扩展到

    function(param1, param2, ..., paramN) . function(param1, param2, ..., paramN)

  • function(std::forward<Tys>(params)...)

    expands to扩展到

    function(std::forward<Tys1>(param1), std::forward<Tys2>(param2), ..., std::forward<TysN>(paramN))

  • function(std::forward<Tys>(params...))

    expands to扩展到

    function(std::forward<Tys>(param1, param2, ..., paramN))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM