Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative function syntax difference

What's the difference between these two functions?

auto func(int a, int b) -> int;

int func(int a, int b);
like image 781
MBZ Avatar asked Dec 30 '12 02:12

MBZ


2 Answers

Other than the notation, there isn't any difference in the above case. The alternative function declaration syntax become important when you want to refer to one or more of the arguments to determine the return type of the function. For example:

template <typename S, typename T>
auto multiply(S const& s, T const& t) -> decltype(s * t);

(yes, it is a silly example)

like image 67
Dietmar Kühl Avatar answered Sep 27 '22 16:09

Dietmar Kühl


There is no useful difference between these two declarations; both functions return an int.

C++11's trailing return type is useful with functions with template arguments, where the return type is not known until compile-time, such as in this question: How do I properly write trailing return type?

like image 45
Johnsyweb Avatar answered Sep 27 '22 16:09

Johnsyweb