std :: string =操作员不能将0作为参数传递
std::string += operator cannot pass 0 as argument
std::string tmp;
tmp +=0;//compile error:ambiguous overload for 'operator+=' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' and 'int')
tmp +=1;//ok
tmp += ' ';//ok...expected
tmp +=INT_MAX;//ok
tmp +=int(INT_MAX);//still ok...what?
第一个人认为将整数作为参数,对吗?为什么其他人通过汇编?我在Visual C 和G 上进行了测试,而我在上面得到了相同的结果。因此,我相信我想念标准定义的东西。是什么?
问题是文字0是 null指针常数。编译器不知道您的意思是:
std::string::operator +=(const char*); // tmp += "abc";
或
std::string::operator +=(char); // tmp += 'a';
(更好的编译器列出选项)。
工作扳机(如您所发现的)是将附加写为:
tmp += ' ';
(我认为您不想要字符串版本-tmp += nullptr;
在运行时是UB。)
0
文字可隐式转换为所有指针类型(导致其各自的空指针常数)。因此,它导致两个同样有效的转换序列,以匹配std::string
S附录操作员。
相关文章:
- 我正在开发服务器,ip作为参数传递不起作用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 将函数类型作为模板参数传递不会编译
- C++ _beginthread不能将字符串作为参数传递
- 混合 c++ 和汇编不能将多个参数从C++函数传递到程序集
- 为什么此参数包不能接受函数指针?
- 为什么模板函数不能作为模板模板参数传递?
- 为什么在传递lambda而不是功能指针时不能推断模板参数
- 为什么当数组作为调用方函数中的参数传递时,不能在被调用函数中使用 foreach 循环打印数组的值
- C++:强制实施函数,使其不能接受按值传递的任何参数
- 为什么我的可变参数宏不能正确接受任何参数?
- std :: string =操作员不能将0作为参数传递
- C++ 不能将公共 const 字符串成员作为参数传递给同一类的成员函数
- 为什么在传递给参数时不能隐式构造给定合适构造函数的对象?
- 为什么不能将字符串文本传递给使用模板参数的函数?
- 对"pthread_key_create"的未定义引用;参数顺序不能解决问题
- 不能将向量作为参数 c++ 传递
- 多参数模板不能很好地处理友元声明
- const变量作为非类型模板参数(variable不能出现在常量表达式中)
- 模板参数无效;不能出现在常量表达式中