c++ - 不明确的 string::operator= 调用隐式转换为 int 和 string 的类型

标签 c++ string implicit-conversion overload-resolution conversion-operator

给定以下程序:

#include <iostream>
#include <string>
using namespace std;
struct GenericType{
   operator string(){
      return "Hello World";
   }
   operator int(){
      return 111;
   }
   operator double(){
      return 123.4;
   }
};
int main(){
   int i = GenericType();
   string s = GenericType();
   double d = GenericType();
   cout << i << s << d << endl;
   i = GenericType();
   s = GenericType(); //This is the troublesome line
   d = GenericType();
   cout << i << s << d << endl;
}

它在 Visual Studio 11 上编译,但不是 clang 或 gcc。它有问题,因为它想从 GenericType 隐式转换为 intchar 但它也可能返回 string 因此存在歧义(operator=(char)operator=(string) 都匹配 GenericType)。

然而,复制构造函数就很好。

我的问题是:如何在不修改 main 的内容的情况下解决这种歧义?我需要做什么来修改 GenericType 来处理这种情况?

最佳答案

我相信 gcc 和 clang 是正确的。

有两个 operator= 重载在起作用:

string& operator=(string const& str); // (1)
string& operator=(char ch);           // (2)

这两个 operator= 重载都需要从类型为 GenericType 的参数进行用户定义的转换。 (1) 需要使用到string 的转换。 (2) 要求使用到 int 的转换,然后是到 char 的标准转换。

重要的是两个重载都需要用户定义的转换。要确定这些转换中的一个是否比另一个更好,我们可以查看重载解析规则,特别是来自 C++11 §13.3.3.2/3 的以下规则(为清楚起见重新格式化):

User-defined conversion sequence U1 is a better conversion sequence than another user-defined conversion sequence U2 if

  1. they contain the same user-defined conversion function or constructor or aggregate initialization and

  2. the second standard conversion sequence of U1 is better than the second standard conversion sequence of U2.

请注意, 连接了规则的两个部分,因此必须满足两个部分。不满足规则的第一部分:两个用户自定义转换序列使用不同的用户自定义转换函数。

因此,两种转换都不是更好,而且调用是有歧义的。

[关于如何在不更改 main() 定义的情况下解决问题,我没有好的建议。隐式转换通常不是一个好主意;它们有时非常有用,但更多时候它们可能会导致过载歧义或其他奇怪的过载行为。]

有一个 gcc 错误报告,其中描述了这个问题,并按设计解决了:compiler incorrectly diagnoses ambigous operator overload.

关于c++ - 不明确的 string::operator= 调用隐式转换为 int 和 string 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10743106/

相关文章:

excel - 如何使用 VBA 将以 = 开头的字符串插入到单元格值中?

c# - 嵌套隐式运算符

c++ - 如何使用带指针的转换构造函数?

c++ - 如何防止从 int 到 unsigned int 的隐式转换?

Java替换 ' " 'character with '\"'

c++ - 使用Eigen::FFT执行FFT时的频率

c++ - 使用 std::reference_wrapper 作为 std::map 中的键

c++ - 无法从 'initializer-list' 转换为 UserController

c - 文件内容到大字符串

c++ - 不断更新 Opengl 窗口