doe*_*195 3 c++ operator-overloading operator-keyword explicit-conversion
我正在尝试使用以下函数为字符串和双定义运算符 +
string operator + (const double& b,const string a){
return to_string(b)+a;
}
Run Code Online (Sandbox Code Playgroud)
当我进行以下操作时,效果很好
double c = 100.256;
string d = "if only";
cout<<c+d<<"\n";
Run Code Online (Sandbox Code Playgroud)
但是当我传递 const char 而不是 string 时,它会抛出编译错误('double'和'const char [4]'类型的无效操作数到二进制'operator+')
double c = 100.256;
string test = c+"sff";
Run Code Online (Sandbox Code Playgroud)
为什么不发生 const char[] "sff" 到字符串的隐式转换?
Vla*_*cow 10
根据 C++ 17 标准(表达式中的 16.3.1.2 运算符)
1 如果表达式中运算符的任何操作数都不是类或枚举类型,则假定该运算符是内置运算符并根据第 8 条进行解释。
在这个表达式中
c+"sff"
Run Code Online (Sandbox Code Playgroud)
(其中c是类型的标量值double)两个操作数都没有类或枚举类型以及类型的内置运算符 +double并且const char *未定义。当第二个操作数具有整数类型时定义指针算术。
| 归档时间: |
|
| 查看次数: |
144 次 |
| 最近记录: |