简体   繁体   中英

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

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))
                          );

Based on my understanding, the pass-in type T for std::logical_and<T> is the type of the pass-in parameters of function operator() . Given the above code, it seems that the type of std::greater is bool that is determined by the returned value of operator() .

Is that correct?

Thank you

The return type of the operator() function is bool. The type of std::greater is std::greater . It's a functional object. Thus:

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

will return whatever your compiler uses to display the instantiation type of a class template: "struct std::greater<int>" for VC++, for example.

The boost binder does a bit more magic than what you might be expecting. When one of the bound arguments is a bind expression itself, it will execute that expression during the call and use the result. In this case, the internal bound expressions are calls to std::less<int> and std::greater<int> , both of which yield a bool , which is then passed to the std::logical_and<bool> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM