C++超载方法的力类型分辨率
我已经继承了与以下几个相似的代码:
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 :: x
和b :: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论