为什么C++找不到布尔运算符,当有一个!范围内的运算符?
Why does C++ not find bool operator when there's a ! operator in scope?
正在处理解析器组合器库,这个例子就是从这个库派生出来的,尽管显然一些名称已经更改以保护无辜者:
#include <string>
#include <stdio.h>
using namespace std;
template <typename T> struct only_string;
template <> struct only_string<string> {};
struct another_type {
explicit operator bool() const { return true; }
};
// only substitute if T is string
template <typename T>
bool operator !(T) {
only_string<T> a;
return true;
}
int main() {
another_type a;
if (!a) {
return 1;
} else {
return 0;
}
}
我有一个模板运算符!,它应该只在 T 是字符串时替换,而另一个类型上有一个布尔运算符。 如果我尝试调用 !a,它会先找到运算符,无法替换并放弃。 谁能解释这种行为以及如何纠正它?
这是 g++ 5.4.0 的输出
> g++ -std=c++11 test.cc -o test
test.cc: In instantiation of ‘bool operator!(T) [with T = another_type]’:
test.cc:24:10: required from here
test.cc:17:20: error: ‘only_string<another_type> a’ has incomplete type
only_string<T> a;
^
是的,编译器"放弃"了,因为它认为 ! 运算符是最好的匹配。如果确实希望编译器忽略该重载,则需要使用一种称为 SFINAE 的技术。
template <typename T,
std::enable_if_t<std::is_same_v<T, std::string>>* = nullptr>
bool operator !(T) {
return true;
}
这样,如果编译器尝试选择此函数,它将无法将参数替换为签名并忽略它。这不会发生在函数的主体中,这就是您的版本失败的原因。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 未在此范围内声明错误 'xy'
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 命名空间范围内的外部 - GCC vs clang vs msvc
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 如何仅使用 While 循环在给定范围内找到可被 7 整除的计数整数
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- 查找给定范围内最长连续 1 的频率
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- 为什么C++找不到布尔运算符,当有一个!范围内的运算符?