C++ force type resolution for overloaded methods

76 Views Asked by At

I have inherited code that looks similar to the following:

struct A {
  typedef void * X;
};

struct B {
  typedef void * Y;
};

struct foo {
  void bar (A::X x, int a, int b) {
    return;
  }

  void bar (B::Y y, int c = 0) {
    return;
  }

  void baz (int i) {
    return;
  }

  void baz (float f) {
    return;
  }
};

int main (int argc, char * argv[]) {
  B::Y my_y;
  foo f;
  f.bar(my_y, 3);

  return 0;
}

I would really like to add default values to void bar (A::X x, int a, int b), but the compiler begins to complain.

$ g++ main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:30:8: error: call of overloaded ‘bar(void*&, int)’ is ambiguous
   30 |   f.bar(my_y, 3);
      |   ~~~~~^~~~~~~~~
main.cpp:10:8: note: candidate: ‘void foo::bar(A::X, int, int)’
   10 |   void bar (A::X x, int a = 0, int b = 0) {
      |        ^~~
main.cpp:14:8: note: candidate: ‘void foo::bar(B::Y, int)’
   14 |   void bar (B::Y y, int c = 0) {
      |        ^~~

Is there a way to sufficiently differentiate the types A::X and B::Y to make this possible?

0

There are 0 best solutions below