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

私の理解に基づいて、パスイン型Tforstd::logical_and<T>は function のパスイン パラメータの型operator()です。上記のコードを考えると、 の型はstd::greaterbool戻り値によって決定されるようです operator()

あれは正しいですか?

ありがとうございました

4

2 に答える 2

2

関数の戻り値の型operator()は bool です。の型は std::greaterですstd::greater。機能的なオブジェです。したがって:

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

クラステンプレートのインスタンス化タイプを表示するためにコンパイラが使用するものは何でも返します"struct std::greater<int>"。たとえば、VC++ の場合です。

于 2012-04-23T16:47:35.037 に答える
1

ブースト バインダーは、予想よりも少し多くの魔法を行います。バインドされた引数の 1 つがバインド式自体である場合、呼び出し中にその式が実行され、結果が使用されます。この場合、内部バインド式は と の呼び出しでstd::less<int>ありstd::greater<int>、どちらも を生成しbool、それが に渡されますstd::logical_and<bool>

于 2012-04-23T16:46:55.613 に答える