[Cs3] using auto with overloaded functions

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Oct 15 15:30:18 EDT 2018


CS3 students,

There was a question a few lectures back as to what happens when there
is an auto type specifier (to be resolved to a function pointer) but
functions are overloaded. As it turns out, nothing good: compiler
complains as expected. 

However, if the function pointer type is specified completely, then
the compiler resolves the correct overload for the function
pointer. Similarly, auto works correctly if the function is not
overloaded (the name is specifies the function unambiguously).

Here is the code to try it out. And here is the page that describes
function address resolution in greater detail.

   https://en.cppreference.com/w/cpp/language/overloaded_address

thanks,
--
Mikhail

--------------
// demonstrates function pointer resolution
// Mikhail Nesterenko
// 10/15/2018

int f(int) { return 1; }
int f(double) { return 2.; }

int main(){
   // auto g = f;   // works only if f is not overloaded
   int (*h)(int) = f; // works regardless of overload
}


More information about the cs3 mailing list