查找模板相等运算符
Finding template equality operators
我想写一个代理类,它接受一个模板值,可以与模板可以比较的任何类进行比较。
template <class T>
class Proxy {
public:
Proxy(T value) : _value(value) {}
template <class U> // this should exist only if the T == U operator is defined
bool operator==(U const& other) const { return _value == other; }
template <class U> // this should exist only if U == T is defined
friend bool operator==(U const& first, Proxy<T> const& second) const { return first == second._value; }
private:
T _value;
};
例如,由于这是法律代码:
bool compare(std::string first, std::string_view second) {
return first == second;
}
我也希望这是合法的:
bool compare(std::string first, Proxy<std::string_view> second) {
return first == second;
}
但澄清一下,这应该适用于任何可以比较的类,或者可以隐式转换以便进行比较的类。我可以定义一个模板条件来检查这两种情况吗?
由于您的标准本质上是像_value == other
这样的表达式是否格式正确,因此您可以依靠表达式 SFINAE 来测试它。
template <class U> // this should exist only if the T == U operator is defined
auto operator==(U const& other) const -> decltype(_value == other)
{ return _value == other; }
template <class U, std::enable_if_t<!std::is_same<U, Proxy>::value, int> = 0> // this should exist only if U == T is defined
friend auto operator==(U const& first, Proxy const& second) -> decltype(first == second._value)
{ return first == second._value; }
它可能不是很干燥,因为我们需要重复两次表达式,但这是一种相当简单的方法来做 SFINAE,这是一个主要的优点。
另一件需要注意的事情是,我们不希望递归地考虑第二个重载,这可能会在比较两个代理时发生。因此,我们需要另一个SFINAE条件,用enable_if
拼写出老式的方式,以便在U
是Proxy
时丢弃过载。这依赖于 C++14 功能,即按声明顺序检查替换项。将其放在首位可防止first == second._value
中的递归。
相关文章:
- 参数相关查找和流运算符重载
- 在类设计中查找外部命名空间中的重载运算符
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
- 移位运算符如何在查找两个整数中的不同位数?
- 运算符<<依赖于参数的查找不在全局命名空间中查找
- 查找模板相等运算符
- 查找所有移动构造函数和移动赋值运算符(特别是那些没有"noexcept"的运算符)
- 放置运算符新查找
- 在没有比较运算符的情况下查找 2 个数字之间的最小值
- 查找运算符重载C++错误
- 使用C++逐位运算符查找匹配的颜色
- 范围运算符需要查找 std 命名空间而不是提升
- 在C 中查找移动分配运算符的所有用途的工具
- 查找矩阵实现的 [] 运算符中的设计缺陷
- 在运算符查找中,成员不优先于非成员
- 输入流运算符查找顺序与Boost.Program_options
- 理解运算符查找;哪个编译器是正确的
- C++运算符查找错误
- 使用逐位运算符查找bmp图像的宽度和高度
- 使用位运算符查找在数组中只出现一次的数字