Hacker News new | ask | show | jobs
by bff 5787 days ago
I've been using them for a while now (gcc has support) and they're not awkward. The ability to specify scope is actually rather nice and most of the time you'll either leave the scope empty with [] or pass everything in by value [=] or reference [&]. The return type of the lambda function is also usually detectable by the compiler so you only need to specify it rarely.
1 comments

To be more specific: the return type of the lambda can be omitted for a lambda whose body is just "return {expression}". The compiler could, if it wanted, always deduce the return type (since implementing type inference would be easy). The fact that it doesn't isn't to make it easy for compilers; it was a design decision of the language: having unspecified return types for complicated pieces of code hurts readability and maintainability.
Return types can't always be deduced since they can be ambiguous - imagine an if statement where one branch returns a float and the other returns a double. The desired return type could really be any numeric value since the types can be converted so the return type does need to be specified. Since C++ has overloaded functions the compiler can't just look at the function that the result is being passed into to determine the type either.
The solution would be then to do autoconversions as appropriate or return an error.

The compiler already deals with this issue with the ?: operator. It's a readability and maintenance problem there, too. It'd be easy to do; the language designers chose not to.