Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A lambda's return type can be deduced by the return value, so why can't a function's?

#include <iostream>  int main(){      auto lambda = [] {         return 7;     };      std::cout << lambda() << '\n';  } 

This program compiles and prints 7.
The return type of the lambda is deduced to the integer type based on the return value of 7.


Why isn't this possible with ordinary functions?

#include <iostream>  auto function(){     return 42; }  int main(){      std::cout << function() << '\n'; } 

error: ‘function’ function uses ‘auto’ type specifier without trailing return type

like image 510
Trevor Hickey Avatar asked Dec 04 '13 05:12

Trevor Hickey


People also ask

What will not be the return type of a function if it returns a value?

When a function does not return a value, void is the type specifier in the function declaration and definition. A function cannot be declared as returning a data object having a volatile or const type, but it can return a pointer to a volatile or const object.

Which return type Cannot return any value to the Cal?

Correct Option: Estruct, void and char * return-type cannot be used for a function in C.

Can the return type of a function be auto?

In C++14, you can just use auto as a return type.

Can Lambda have a return type?

The return type of a lambda expression is automatically deduced. You don't have to use the auto keyword unless you specify a trailing-return-type. The trailing-return-type resembles the return-type part of an ordinary function or member function.


2 Answers

C++14 has this feature. You can test it with new versions of GCC or clang by setting the -std=c++1y flag.

Live example

In addition to that, in C++14 you can also use decltype(auto) (which mirrors decltype(auto) as that of variables) for your function to deduce its return value using decltype semantics.

An example would be that for forwarding functions, for which decltype(auto) is particularly useful:

template<typename function_type, typename... arg_types> decltype(auto) do_nothing_but_forward(function_type func, arg_types&&... args) {     return func(std::forward<arg_types>(args)...); } 

With the use of decltype(auto), you mimic the actual return type of func when called with the specified arguments. There's no more duplication of code in the trailing return type which is very frustrating and error-prone in C++11.

like image 174
Mark Garcia Avatar answered Sep 22 '22 13:09

Mark Garcia


This is just a limitation of how the language was created and has evolved. In the upcoming C++14 standard the return type of a function can be deduced in some contexts, although not in all. There are complications when there are multiple return statements.

Additionally, deduced return types have other issues, for example, the return type of a template function cannot be used in a SFINAE context, as to be able to deduce the type, the compiler must instantiate the function template, which happens after substitution. The net result is that while the feature will be there in the near future, I would avoid it if you can provide the type yourself.

like image 43
David Rodríguez - dribeas Avatar answered Sep 21 '22 13:09

David Rodríguez - dribeas