Search code examples
c++templatesc++20template-argument-deduction

Is there any difference between a deduced template arg and auto arg?


Are these two functions different in any meaningful way? Is there any reason to generally prefer one over the other?

void foo(auto x, auto &... y) { /* ... */ }

template<typename T, typename... Tx>
void foo(T x, Tx &... y) { /* ... */ }

I would tend to use the first when I don't need the type T handy because it's shorter... but I'm wondering if there are any drawbacks.


Solution

  • It is literally defined to be equivalent [dcl.fct]

    An abbreviated function template is a function declaration that has one or more generic parameter type placeholders. An abbreviated function template is equivalent to a function template whose template-parameter-list includes one invented type template-parameter for each generic parameter type placeholder of the function declaration, in order of appearance.

    Where generic type placeholders are the auto.

    As with most equivalent syntax, it comes down to convention: pick one and stick with it.