7 ms·
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, alway
by okmjuhb 16y ago
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.
- bff 16y agoReturn 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.
- okmjuhb 16y agoThe 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.