c++ - Abbreviated function template vs. function template with forwarding reference param -


what differences between function templates forwarding reference parameters

template<typename t> void universal_func(t && a) { } 

and abbreviated function templates?

void auto_fun(auto && a) { } 

can replace universal_func auto_fun? universal_func of auto_fun or equal?

i have tested below program. seems both same.

template<typename t> void universal_func(t && a) { }  void auto_fun(auto && a) { }  int main() {   int i;      const int const_i = 0;    const int const_ref =const_i;    //forwarding reference template function example     universal_func(1); //call void universal_func<int>(int&&)   universal_func(i);//call void universal_func<int&>(int&):   universal_func(const_i); //call void universal_func<int const&>(int const&)   universal_func(const_ref);//call void universal_func<int const&>(int const&)    //auto calls     auto_fun(1); //call void auto_fun<int>(int&&)   auto_fun(i);//call void auto_fun<int&>(int&):   auto_fun(const_i); //call void auto_fun<int const&>(int const&)   auto_fun(const_ref);//call void auto_fun<int const&>(int const&)   return 0; } 

universal_func , auto_fun deduced , expanded similar functions.

void universal_func<int>(int&&):         pushq   %rbp         movq    %rsp, %rbp         movq    %rdi, -8(%rbp)         nop         popq    %rbp         ret void universal_func<int&>(int&):         pushq   %rbp         movq    %rsp, %rbp         movq    %rdi, -8(%rbp)         nop         popq    %rbp         ret void universal_func<int const&>(int const&):         pushq   %rbp         movq    %rsp, %rbp         movq    %rdi, -8(%rbp)         nop         popq    %rbp         ret void auto_fun<int>(int&&):         pushq   %rbp         movq    %rsp, %rbp         movq    %rdi, -8(%rbp)         nop         popq    %rbp         ret void auto_fun<int&>(int&):         pushq   %rbp         movq    %rsp, %rbp         movq    %rdi, -8(%rbp)         nop         popq    %rbp         ret void auto_fun<int const&>(int const&):         pushq   %rbp         movq    %rsp, %rbp         movq    %rdi, -8(%rbp)         nop         popq    %rbp         ret 

are there differences? standard say?

auto in function parameters not part of standard c++ yet, recent versions of gcc allow extension part of support concepts ts.

the concepts ts refers construct abbreviated function template (although used known generic function, guess generic term). rules perhaps large dump answer, have in [dcl.fct]/16-19 in this draft gory details.

paragraph 16 provides decent overview:

an abbreviated function template function declaration parameter-type-list includes 1 or more placeholders (7.1.6.4). abbreviated function template equivalent function template (14.6.6) template-parameter-list includes 1 invented template-parameter each occurrence of placeholder in parameter-declaration-clause, in order of appearance, according rules below. [ note: template parameters invented deduce type of variable or return type of function when declared type contains placeholders (7.1.6.4.1). — end note ]

by rules set forth in draft, 2 definitions functionally equivalent.

we take function placeholder parameter:

void auto_fun(auto && a) { } 

and invent template parameter replace with:

template <typename t> void auto_fun (t && a) { } 

as can see, has same signature function without placeholders:

template <typename t> void universal_func(t && a) { } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -