0

I can't find out why calls of f are ambigiuous :/ I know that the three declarations are ok but in this case this isn't working..

#include <iostream>

using namespace std;
void f(int);
void f(int &);
void f(const int &);

void f(int a);

void f(int &a);

   void f(const int &a);


int main()
{
    int i=1;
    const int ic=2;
    int &ri=i;
    const int & rc=ic;

    f(i);
    f(ic);
    f(ri);
    f(rc);

    return 0;
}

1 Answers1

1

I know that the three declarations are ok

How? All arguments are convertible to int. The ambiguity is clear.

Do you think int x = rc; won't compile?

I think your confusion stems from void f(int);. Since the parameter is passed by value, anything that can be copied in a new int matches this overload.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I actually think the confusion came from the answer to this question that makes it look like those three are unambiguous: http://stackoverflow.com/questions/10122294/ambigious-functions-in-c – KillianDS Apr 17 '12 at 11:22