Null<std::string> 的问题
Issue with Null<std::string>
我在 Null 类下面设计了泛型编程,我可以做一些类似 T A=Null() 的事情,除了 std::string,编译器找不到合适的运算符 == 并给我很多错误。问题是为什么其他类型可以正常工作?我做错了什么?
struct Null
{
operator std::string() const { return std::string{}; }
operator int() const { return 0; }
};
int main() {
std::string s = "hello";
Null n;
std::cout << (0 == n) << std::endl; // works
std::cout << (n == 0) << std::endl; // works
std::cout << (s == n) << std::endl; // error: no match for operator==
}
这里使用的==
实际上是:
template< class CharT, class traits, class Alloc >
bool operator==( const basic_string<CharT,Traits,Alloc>& lhs,
const basic_string<CharT,Traits,Alloc>& rhs );
用户定义的转化序列不考虑模板类型推断,因此无法在此处(或其他)推断出CharT
参数。
要解决此问题,您可能需要定义自己的非模板operator==
。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题