c++转换运算符重载、枚举、整数和字符

发布于 2024-12-04 16:01:50 字数 867 浏览 5 评论 0原文

当我尝试编译(使用 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2024-12-11 16:01:50

MyClass 的转换是不明确的,因为有一个到 int 的转换,还有一个到 enum 的转换,而枚举本身可以隐式转换为 int,并且两者都是同样好的转换。不过,您可以通过指定所需的转换来明确调用:

myfunc(int(MyClass()));

或者,您可能想重新思考为什么您有一个对 intchar 有单独重载的函数>,也许也可以重新设计。

The conversion from MyClass is ambiguous, as there is one conversion to int and one to the enum, which is itself implicitly convertible to int, and both are equally good conversions. You can just make the call explicit, though, by specifying which conversion you want:

myfunc(int(MyClass()));

Alternatively, you might like to rethink why you have a function that has separate overloads for int and char, perhaps that could be redesigned as well.

如何视而不见 2024-12-11 16:01:50

enum 是 C++ 中的类型,与 C 不同。

enum -> 都有隐式转换。 charenum -> 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 to char. Test here.

  • void myFunc(int) 的定义被删除时,编译失败。
  • Compiler tries to convert from MyClass to char and finds that, not having a user defined conversion operator char(), both user defined int() and SimpleEnum() may be used. Test here.

  • 当您为 MyClass 添加 char() 转换运算符时,编译会失败,并出现与未添加相同的错误。
  • Test here.

因此,我在这里得出的结论是,在您最初发布的代码中,编译器必须决定应该调用 myFunc 的两个重载版本中的哪一个。
由于这两种转换都是可能的:

  1. 通过用户定义的转换运算符将 MyClass 转换为 int
  2. 通过用户定义的转换(MyClassSimpleEnum)+ 隐式转换(SimpleEnum)将 MyClass 转换为 int code> 到 char)

编译器不知道使用哪一个。

enums are types in C++, unlike C.

There are implicit conversions for both enum -> char and enum -> int. The compiler just doesn't know which one to choose.


EDIT: After trying with different tests:

  • When the definition for custom conversion 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 to char. Test here.

  • When the definition for void myFunc(int) is removed compilation fails.
  • Compiler tries to convert from MyClass to char and finds that, not having a user defined conversion operator char(), both user defined int() and SimpleEnum() may be used. Test here.

  • When you add a char() conversion operator for MyClass 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:

  1. MyClass to int via user defined conversion operator.
  2. MyClass to int via user defined conversion (MyClass to SimpleEnum) + implicit conversion (SimpleEnum to char)

compiler knows not which one to use.

━╋う一瞬間旳綻放 2024-12-11 16:01:50

我本来期望调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文