c++转换运算符重载、枚举、整数和字符
当我尝试编译(使用 gcc 4.3.4)此代码片段时:
enum SimpleEnum {
ONEVALUE
};
void myFunc(int a) {
}
void myFunc(char ch) {
}
struct MyClass {
operator int() const { return 0; };
operator SimpleEnum() const { return ONEVALUE; };
};
int main(int argc, char* argv[]) {
myFunc(MyClass());
}
我收到此错误:
test.cc: In function "int main(int, char**)":
test.cc:17: error: call of overloaded "myFunc(MyClass)" is ambiguous
test.cc:5: note: candidates are: void myFunc(int)
test.cc:8: note: void myFunc(char)
我想我(几乎)明白问题是什么,即(简化很多)即使我谈论“char”和“ enum”,它们都是整数,然后重载是不明确的。
无论如何,我不太明白的是,如果我删除 myFunc OR 的第二个重载 MyClass 的转换运算符之一,我没有编译错误。
由于这个问题我将更改很多旧代码(我正在将代码从旧版本的 HP-UX aCC 移植到 Linux 下的 g++ 4.3.4),所以我想更好地理解整个事情以便选择修改代码的最佳方式。
预先感谢您的帮助。
When I try to compile (with gcc 4.3.4) this code snippet:
enum SimpleEnum {
ONEVALUE
};
void myFunc(int a) {
}
void myFunc(char ch) {
}
struct MyClass {
operator int() const { return 0; };
operator SimpleEnum() const { return ONEVALUE; };
};
int main(int argc, char* argv[]) {
myFunc(MyClass());
}
I get this error:
test.cc: In function "int main(int, char**)":
test.cc:17: error: call of overloaded "myFunc(MyClass)" is ambiguous
test.cc:5: note: candidates are: void myFunc(int)
test.cc:8: note: void myFunc(char)
I think I (almost) understand what the problem is, i.e. (simplifying it a lot) even if I speak about "char" and "enum", they all are integers and then the overloading is ambiguous.
Anyway, the thing I don't really understand is that if I remove the second overloading of myFunc OR one of the conversion operators of MyClass, I have no compilation errors.
Since I'm going to change A LOT of old code because of this problem (I'm porting code from an old version of HP-UX aCC to g++ 4.3.4 under Linux), I would like to understand better the whole thing in order to choose the best way to modify the code.
Thank you in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
MyClass
的转换是不明确的,因为有一个到int
的转换,还有一个到 enum 的转换,而枚举本身可以隐式转换为int
,并且两者都是同样好的转换。不过,您可以通过指定所需的转换来明确调用:或者,您可能想重新思考为什么您有一个对
int
和char
有单独重载的函数>,也许也可以重新设计。The conversion from
MyClass
is ambiguous, as there is one conversion toint
and one to the enum, which is itself implicitly convertible toint
, and both are equally good conversions. You can just make the call explicit, though, by specifying which conversion you want:Alternatively, you might like to rethink why you have a function that has separate overloads for
int
andchar
, perhaps that could be redesigned as well.enum
是 C++ 中的类型,与 C 不同。enum
-> 都有隐式转换。char
和enum
->int
。编译器只是不知道选择哪一个。编辑:尝试不同的测试后:
MyClass
->int
被删除,代码可以编译。Here there is implicit conversion for enum to
int
and so it is the one favored by the compiler over the one tochar
. Test here.void myFunc(int)
的定义被删除时,编译失败。Compiler tries to convert from
MyClass
tochar
and finds that, not having a user defined conversion operatorchar()
, both user definedint()
andSimpleEnum()
may be used. Test here.MyClass
添加char()
转换运算符时,编译会失败,并出现与未添加相同的错误。Test here.
因此,我在这里得出的结论是,在您最初发布的代码中,编译器必须决定应该调用
myFunc
的两个重载版本中的哪一个。由于这两种转换都是可能的:
MyClass
转换为int
。MyClass
到SimpleEnum
)+ 隐式转换(SimpleEnum
)将MyClass
转换为int
code> 到char
)编译器不知道使用哪一个。
enum
s are types in C++, unlike C.There are implicit conversions for both
enum
->char
andenum
->int
. The compiler just doesn't know which one to choose.EDIT: After trying with different tests:
MyClass
->int
is removed, code compiles.Here there is implicit conversion for enum to
int
and so it is the one favored by the compiler over the one tochar
. Test here.void myFunc(int)
is removed compilation fails.Compiler tries to convert from
MyClass
tochar
and finds that, not having a user defined conversion operatorchar()
, both user definedint()
andSimpleEnum()
may be used. Test here.char()
conversion operator forMyClass
compilation fails with the same error as if not.Test here.
So the conclusion I come up with here is that in your originally posted code compiler has to decide which of the two overloaded versions of
myFunc
should be called.Since both conversions are possible:
MyClass
toint
via user defined conversion operator.MyClass
toint
via user defined conversion (MyClass
toSimpleEnum
) + implicit conversion (SimpleEnum
tochar
)compiler knows not which one to use.
我本来期望调用 int 重载。尝试了几个编译器并得到了不同的结果。如果您要删除任何内容,请将用户转换运算符删除为
int
,因为枚举具有到整数的标准转换。I would have expected that the int overload is called. Tried a few compilers and got different results. If you are going to remove anything, remove the user conversion operator to
int
, since enums have a standard conversion to ints.