标准库函数对象的通常模式是有一个带有非模板的模板结构operator()
。例如,std::less
看起来像这样:
template <typename T>
struct less
{
bool operator()(const T& lhs, const T& rhs) const {
return lhs < rhs;
}
};
std::vector<float> vec = ...;
std::sort(vec.begin(), vec.end(), less<float>{});
我的问题是,为什么这比带有模板的非模板结构更好operator()
?似乎上述仿函数在操作上等同于:
struct less2
{
template <typename T>
bool operator()(const T& lhs, const T& rhs) const {
return lhs < rhs;
}
};
std::vector<float> vec = ...;
std::sort(vec.begin(), vec.end(), less2{});
除了我们得到自动类型扣除的奖金。更好的是,如果我们愿意,我们可以比较不同的类型,前提是这样做有意义:
struct less
{
template <typename T, typename U>
bool operator()(const T& lhs, const U& rhs) const {
return lhs < rhs; // compile error if operator<(T, U) is not defined
}
};
例如,从那里很明显我们可以如何使用decltype
来获得真正的通用std::plus
。但是标准库不会那样做。
当然,我确信我不是第一个发生这种情况的人,而且我确信标准委员会决定采用第一种模式而不是第二种模式是有充分理由的。我只是不确定它是什么。有哪位大师能开导我吗?