C++超载方法的力类型分辨率

发布于 2025-02-04 18:44:05 字数 1063 浏览 1 评论 0原文

我已经继承了与以下几个相似的代码:

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;
}

我真的希望将默认值添加到void bar(a :: x x,int a,int b),但是编译器开始抱怨。

$ 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) {
      |        ^~~

有没有一种方法可以充分区分类型a :: xb :: y使之成为可能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文