什么是“std::greater”或“std::less_equal”的类型

what is the type of `std::greater` or `std::less_equal`

本文关键字:std equal 类型 greater 什么 less      更新时间:2023-10-16
template <class T> struct greater : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x>y;}
};
template <class T> struct logical_and : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x&&y;}
};
// (i > 5 && i <=10)
countBoost = std::count_if(vecInts.begin(), vecInts.end(),
                           boost::bind(std::logical_and<bool>(), 
                                                        ^^^^ // ???? Why ????
                                    boost::bind(std::greater<int>(),    _1, 5),
                                    boost::bind(std::less_equal<int>(), _1, 10))
                          );

根据我的理解,std::logical_and<T>的传入类型T是函数operator()的传入参数的类型。给定上面的代码,似乎bool std::greater的类型由返回值 operator() 确定。

这是对的吗?

谢谢

operator()函数的返回类型为 bool。 的类型 std::greaterstd::greater. 它是一个功能对象。 因此:

std::greater<int> g;
std::cout << typeof( g ).name() << std::endl;

将返回编译器用于显示实例化的任何内容类模板的类型:对于 VC++,"struct std::greater<int>"例。

增强活页夹的作用比您预期的要多一些。当其中一个绑定参数是绑定表达式本身时,它将在调用期间执行该表达式并使用结果。在这种情况下,内部绑定表达式是对 std::less<int>std::greater<int> 的调用,两者都产生一个bool,然后传递给std::logical_and<bool>