Why can't the function type be returned automatically?

My question is: why cannot the type of the returned function be deduced ?, or more simply, why the following code gives an error:

auto myfunc(int a) { int a = 12; return a; } 

Why is this invalid?

+5
source share
2 answers

This is a function in C ++ 14, you can try it with GCC 4.9 or clang by setting the -std = C ++ 1y flag.

Live Example: http://coliru.stacked-crooked.com/a/00b8b708d6f0f45b

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3638.html

+2
source

This is allowed in C ++ 14 (and is called automatic type return), you can enable it in your compiler with std=c++1y now.

You can use the return type if your compiler supports C ++ 11 but not C ++ 14:

 auto myfunc(int a) -> int { int b = a; return a; } 
+2
source

Source: https://habr.com/ru/post/1210359/


All Articles