What's the difference between these two functions?
auto func(int a, int b) -> int;
int func(int a, int b);
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)
template <typename S, typename T> decltype(s * t) multiply(S const& s, T const& t);
Do you know why?
friend
functions inside a class template as these cannot be defined outside the class definition).
Dec 30, 2012 at 2:25
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?
They use different syntax and only one of them is valid in C++ versions prior to C++11. Other than that there are no differences between the two function declarations that you've shown in your question.