3 ms·
With Concepts, we will also have "auto" in function arguments. It avoids "template <typename ...>" for simple functions. I already use it as a "catch all" over
by vmorgulis 11y ago
With Concepts, we will also have "auto" in function arguments. It avoids "template <typename ...>" for simple functions.
I already use it as a "catch all" over specialized functions (gcc -std=c++17).
- repsilat 11y agoWhy prefer int foo(auto a, auto &&b); to int foo(a, &&b); ? Too big a change? Too much magic? Maybe more difficult to read or parse. The argument "It's not clear that the function is a template" isn't there any more, though, so it seems reasonable to me.
- kevinnk 11y agoYou actually hit on the exact reason; your change is significantly harder to parse. C++ allows you to alias typenames and variable names, so you wouldn't know if `int foo(T);` was a declaration of a function that take a type `T` or a function that has an implicitly typed parameter called `T` until much later in the parse. Adding auto makes it clear this is a declaration of a variable. That kind of ambiguity already exists in C++[1], but is widely regarded as one of the things that makes parsing C++ extremely hairy. Interestingly, I remember hearing about this exact problem in the context of Swift's closures; if you want type inference, you need to leave off the parens around the parameters. This is mainly done (IIRC) because it's easier for the parser. [1] http://blog.reverberate.org/2013/08/parsing-c-is-literally-undecidable.html http://blog.reverberate.org/2013/08/parsing-c-is-literally-u...
- vmorgulis 11y agoDo you mean? template <typename T1,typename T2> int foo(T1 a, T2 &&b); "auto" is shorter.