Hacker News new | past | comments | ask | show | jobs | submit login

Why 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.



You 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-u...


Do you mean?

    template <typename T1,typename T2>
    int foo(T1 a, T2 &&b);
"auto" is shorter.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: