It's simple really - foo
at that point only means foo(const int& f)
. There's no second choice. Not yet. Switch up the definitions. Or separate them:
int foo(const int& f);
int& foo(int& f);
int main()
{
int f = 11;
cout << foo(f) << endl;
cout << foo(22) << endl;
}
int foo(const int& f) //version 1
{
int g = f;
return int(foo(g)); // calls itself, turning into SO
}
int& foo(int& f) //version 2
{
f *= -1;
return f;
}