Abbreviated function template vs. function template with forwarding reference param

前端 未结 1 2163
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 14:52

What are the differences between function templates with forwarding reference parameters

template
void Universal_func(T &         


        
1条回答
  •  心在旅途
    2021-02-15 15:08

    auto in function parameters is not part of standard C++ yet, but some recent versions of GCC allow this as an extension as part of their support for the Concepts TS.

    The Concepts TS refers to this construct as an abbreviated function template (although it used to be known as a generic function, which I guess was too generic a term). The rules are perhaps too large to dump into this answer, but have a look in [dcl.fct]/16-19 in this draft for all the gory details.

    Paragraph 16 provides a decent overview:

    An abbreviated function template is a function declaration whose parameter-type-list includes one or more placeholders (7.1.6.4). An abbreviated function template is equivalent to a function template (14.6.6) whose template-parameter-list includes one invented template-parameter for each occurrence of a placeholder in the parameter-declaration-clause, in order of appearance, according to the rules below. [ Note: Template parameters are also invented to deduce the type of a variable or the return type of a function when the declared type contains placeholders (7.1.6.4.1). — end note ]

    By the rules set forth in that draft, your two definitions are functionally equivalent.

    We take the function with a placeholder parameter:

    void auto_fun(auto && a)
    {
    }
    

    And invent a template parameter to replace it with:

    template 
    void auto_fun (T && a)
    {
    }
    

    As you can see, this has the same signature as your function without placeholders:

    template 
    void Universal_func(T && a)
    {
    }
    

    0 讨论(0)
提交回复
热议问题