Search code examples
c++c++14autoc++17

auto parameter type in functions


I would like to know if the standard committee considered expanding the C++14 auto keyword to deduce function template parameter type, as it exists today in generic lambdas. (as can be seen nicely depicted in this answer)

Because it works in lambda functions, it should also work in any function. Of course it would be totally redundant with the classic syntax:

template< typename T >
void f(T param);

But being able to write this, for the same result:

void f(auto param);

I think would allow for less clogged code (shorter neater cleaner) and allow a great consistency in this use case:

auto v = func1();
f(v);

As you can see, we used the auto type deducer to declare v, but then we have to use either a hard-typed parameterized function f, or a templated f.
In combination with auto we should use auto, that would be more consistent.

EDIT: this question indeed asks effectively the same thing, but less directly. And has yet to get the answer that user657267 gives, which I reproduce and extend thereunder.


Solution

  • Ok, so thanks to Piotr pointing this other question asking about the same thing, I found the information in a comment that will resolve this, here it is:

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4040.pdf

    page 16, chapter 5.1.1 named generic functions

    A generic function is a function template whose template-parameter-list has a parameterdeclaration whose type-specifier is either auto or a constrained-type-name.

    [ Example:

    auto f(auto x); // Ok
    void sort(C& c); // Ok (assuming C names a concept)
    

    — end example ]

    This seems rather positive :)

    followed by the excpected obvious wording, that matches generic lambda:

    The declaration of a generic function has a template-parameter-list that consists of one invented type template-parameter for each occurrence of auto.

    [ Example: The following generic function declarations are equivalent:

    template<typenaem T>  
    conxtexpr bool C() { ... }  
    auto f(auto x, const C& y);  
    template<typename T1, C T2> 
    auto f(T1 x, const T2& y);
    

    The type of y is a type parameter constrained by C. — end example ]