c++ - 为什么 std::compare_three_way 不是模板结构/函子
问题描述
例如,比较的std::less
被定义为模板结构
template< class T = void >
struct less;
Whilestd::compare_three_way
被定义为一个普通的结构,它operator()
是一个模板函数。(来自 MSVC 的代码)
struct compare_three_way {
template <class _Ty1, class _Ty2>
requires three_way_comparable_with<_Ty1, _Ty2> // TRANSITION, GH-489
constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
noexcept(noexcept(_STD forward<_Ty1>(_Left) <=> _STD forward<_Ty2>(_Right))) /* strengthened */ {
return _STD forward<_Ty1>(_Left) <=> _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
那么为什么不是std::compare_three_way
模板结构呢?
template <class _Ty1, class _Ty2>
requires three_way_comparable_with<_Ty1, _Ty2>
struct compare_three_way {
constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const;
};
顺便说一句,我可以在我自己的容器实现中替换为,比如std::less<T>
在C# 中。std::three_way_compare
Comparer<T>
解决方案
原始std::less
(及其朋友)的比较定义如下:
bool operator()( const T& lhs, const T& rhs ) const;
由于此函数调用运算符不是模板,它只能比较用于实例化std::less
模板的类型的对象。
在后来的语言版本中,这些比较器进行了扩展,专门std::less<>
通过模板化函数调用运算符本身来支持比较不同类型的对象:
template< class T, class U>
constexpr auto operator()( T&& lhs, U&& rhs ) const
-> decltype(std::forward<T>(lhs) < std::forward<U>(rhs));
这在很大程度上淘汰了std::less<T>
大多数用例中的同构版本 ( ),因为它要么是等效的,要么由于不强制转换为通用类型而更有效。保留旧的同类比较器以实现向后兼容性。
异构查找在std::compare_three_way
提出时是一个存在的东西,因此从未引入同构版本。
要在模板中使用它,
std::less<T>
例如std::set<T>
您可以使用std::compare_three_way
in std::set<T>
等std::set<U>
(象征性地),就像您可以使用std::less<>
. 你(可能)不需要std::less<T>
也不 std::less<U>
- 也不std::compare_three_way<T>
存在。
推荐阅读
- haskell - 映射然后在 Haskell 中过滤
- graphics - 在不同的队列族上重用相同的主机可见缓冲区
- javascript - 赛普拉斯如何关闭窗口打印对话框
- r - 如果今天的日期是周六、周日或周一,则将今天的日期设置为上周五,否则在 R 中设置为今天
- ios - HSL 流视频在 IOS 15 上经过一段时间后拉伸
- python - tkinter - wm_protocol 处理 WM_HELP 消息
- react-native - 传递 Props 来创建MaterialTopTabNavigator 屏幕组件
- nginx - 当我使用 nginx 时,Nuxt-mail 不发送电子邮件
- android - Expo app FCM后台推送通知:如何从app发送?
- r - 尝试将自定义位置调整添加到 ggplot2 中的包时出错