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)
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With