You can do something like this:
int main()
{
int a = 42;
void (*fp)(int&) = &f; // (A)
fp(a);
}
Here, we're taking a function pointer to one of the overloaded functions f
. As explained in my answer to another question, the compiler will select the proper function to make line (A) work. Note that I didn't use a cast; that way if I change the function signatures such that the above doesn't work the compiler will issue an error instead of potentially allowing the code to invoke undefined behavior.
Although for this situation, it's better to simply give the two functions different names. Like so:
void take_f(int) {}
void modify_f(int&) {}
int main()
{
int a = 42;
modify_f(a);
}
This allows for code with a clearer intent and you won't have overloading issues. Of course, you can come up with better names than take_f()
and modify_f()
.
It makes more sense this way anyway since having one function takie an int
and another an int&
is probably a good sign that they are doing significantly different things (and thus warrant different function names).