为什么C++关联容器谓词默认不透明
Why C++ associative containers predicate not transparent by default?
自 C++14 以来,我们std::less<void>
在大多数情况下是透明且更有用的,所以是否有原因,例如,默认情况下,std::set
仍然将std::less<Key>
作为谓词,而不是std::less<void>
,除非历史原因。
有用案例:std::set<std::string>::find
std::string_view
等。
这样做会破坏当前的工作代码。 想象一下我有
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;
如果std::set
更改为使用std::less<void>
则此代码将不再编译,因为my_type
没有operator <
。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如何使用默认参数等选择模板专业化
- 具有默认模板参数的多态类的模板推导失败
- std::condition_variable::wait()如何评估给定的谓词
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 初始化具有非默认构造函数的std::数组项的更好方法
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 何时提供默认参数作为模板参数
- 是默认情况下分配给char数组常量的值
- 具有默认值的引用获取函数
- 具有默认模板类型的默认构造函数的类型推导
- 当给定默认值时,为什么此模板参数推导失败
- 修改 VS Code 中的默认C++代码段
- 声明默认的模板化函数
- 将const引用参数初始化为默认参数会导致悬空引用吗
- 如何使用非默认构造函数实例化模板化类
- 如何修复带有 clang 的参数'args'缺少默认参数的问题?
- 为什么C++关联容器谓词默认不透明
- 使用非默认比较谓词的集合容器