在 C + + 14中,关联容器似乎已经从 C + + 11-[ sociative.reqmts ]/13发生了变化:
除非
Compare::is_transparent
类型存在,否则成员函数模板find
、count
、lower_bound
、upper_bound
和equal_range
不应参与过载分辨率。
使比较器“透明”的目的是什么?
C + + 14还提供了类似下面这样的库模板:
template <class T = void> struct less {
constexpr bool operator()(const T& x, const T& y) const;
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
template <> struct less<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
-> decltype(std::forward<T>(t) < std::forward<U>(u));
typedef *unspecified* is_transparent;
};
例如,std::set<T, std::less<T>>
有一个透明的比较器,而 std::set<T, std::less<>>
有一个。
这解决了什么问题? 这是否改变了标准容器的工作方式?例如,std::set
的模板参数仍然是 Key, Compare = std::less<Key>, ...
,那么默认集是否会丢失其 find
、 count
等成员?