What's the difference between these two functions?
auto func(int a, int b) -> int;
int func(int a, int b);
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)
class A { void a() { b(); } void b(); };
So I thought that this is not a very strict design rule. –
Githens friend
functions inside a class template as these cannot be defined outside the class definition). –
Olympie decltype
return types would have been a nice feature... But one cannot have everything ;) –
Githens 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.
© 2022 - 2024 — McMap. All rights reserved.
template <typename S, typename T> decltype(s * t) multiply(S const& s, T const& t);
Do you know why? – Githens