MBZ
MBZ

Reputation: 27592

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);

Upvotes: 7

Views: 2245

Answers (3)

johnsyweb
johnsyweb

Reputation: 141800

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?

Upvotes: 5

Dietmar Kühl
Dietmar Kühl

Reputation: 153830

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)

Upvotes: 10

sepp2k
sepp2k

Reputation: 370162

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.

Upvotes: 2

Related Questions