6

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.

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
v.oddou
  • 6,476
  • 3
  • 32
  • 63
  • This will be in C++17. – Stephan Dollberg Jul 27 '15 at 08:39
  • [Is auto as a parameter in a regular function a GCC 4.9 extension?](http://stackoverflow.com/q/25879705/3953764) – Piotr Skotnicki Jul 27 '15 at 08:42
  • Interesting, who would have thought we could see something like that in C++ :) – Andre Jul 27 '15 at 09:08
  • now the remaining "question in the question" would be to have a hint about the level of likeliness that this will actually end up in C++17. Unfortunately it seems it got dragged along a big new feature: concepts. I didn't even know concepts were being considered at all. It makes me feel that including concepts now would be a big change, and poses a risk to this generic function feature of being dropped along if concepts are dropped. I feel it would have a nicer place alone it its own right... :/ – v.oddou Jul 27 '15 at 09:16

1 Answers1

6

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 ]

v.oddou
  • 6,476
  • 3
  • 32
  • 63