Why normal functions can not have "auto" arguments if lambda expressions can in C ++?

1

Today, I noticed that lambda expressions accept arguments of type auto for example:

auto add = [](const auto& x, const auto& y) 
{
    return x + y;
}

So I tried with normal functions and it did not work:

auto add(const auto& x, const auto& y)
{
    return x + y; //não compila
} 

Can anyone explain why?

    
asked by anonymous 29.08.2018 / 03:57

1 answer

2

auto for lambda parameters exists to cover the fact that there is no way to specify generic types using template (the syntax would be strange). Well, that has changed in , with the possibility of specifying them within lambda :

auto sum = []<typename T, typename U>(T a, U b) { return a + b; };

There is no auto for normal function parameters, because you can already specify generic types with template s:

template <typename T, typename U>
auto sum(T a, U b) -> decltype(a + b) { return a + b; }

It is a matter of lack of proposals to the committee itself. As far as I know, only an old TS of concepts proposed this use of auto , but it was removed.

    
29.08.2018 / 15:22