从 C++14 开始,我们有std::less<void>
在大多数情况下这是透明且更有用的,那么是否有原因,例如,std::set
仍然有std::less<Key>
默认情况下作为谓词,而不是std::less<void>
除历史原因外。
有用的案例:std::set<std::string>::find
with std::string_view
, etc.
这样做会破坏当前的工作代码。想象一下我有
struct my_type
{
int id;
int bar;
};
namespace std {
template<>
struct less<my_type>
{
bool operator()(my_type const& lhs, my_type const& rhs)
{
return lhs.id < rhs.id; // bar doesn't need to be compared, only need unique id's in the container.
}
};
}
std::set<my_type> foo;
If std::set
被改为使用std::less<void>
那么这段代码将不再编译my_type
没有operator <
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)