【发布时间】:2013-12-17 12:26:54
【问题描述】:
在 C++14 中,关联容器似乎从 C++11 发生了变化——[associative.reqmts]/13 说:
成员函数模板
find、count、lower_bound、upper_bound和equal_range不应参与重载决议,除非Compare::is_transparent类型存在。
使比较器“透明”的目的是什么?
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等成员?
【问题讨论】:
-
例如,see this cppreference description。而且我现在感觉很愚蠢,因为我注意到“成员函数模板”这个词......
-
cppreference 在en.cppreference.com/w/cpp/utility/functional/less_void也有一个简介