Forum Replies Created
-
AuthorPosts
-
8. August 2024 at 19:57 in reply to: Why isn’t auto allowed in a definition where the declaration specifies the type #635579::
AFAIK, The return type of a function has no effect on function overloading, therefore the same function signature with different return type will not be overloaded.
Two signatures by the same name, including argument dependent lookup (ADL), may be considered overloads. The same function signature with different return type (return type overload) isn’t valid C++. Consider the following;
int foo(int a) { return a; } double foo(int a) { return static_cast<double>(a); } // compiler will throw an error
In this case, you’d say the function overloads on the type (or tries to do so as only the return type differs). And that’s not valid C++.
8. August 2024 at 18:42 in reply to: Why isn’t auto allowed in a definition where the declaration specifies the type #635577::Absolutely, but this isn’t a case of actually overloading the return type (which
int foo()
vsdouble foo()
would be). What I am curious about is the reason why the compiler isn’t allowed to deduce the return type and match. Because clearly, the function in the example is the same —auto
will be deduced toint
.By the way, if I use trailing return in the definition (and not in the declaration), it compiles as expected, even if
auto
is in there :) -
AuthorPosts