17

binary_functionC++11から削除されていることがわかりました。なぜだろうと思います。

C++98:

template <class T> struct less : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const {return x<y;}
};

C++11:

template <class T> struct less {
  bool operator() (const T& x, const T& y) const {return x<y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

変更-------------------------------------------------- ---------------------------

template<class arg,class result>
struct unary_function
{
       typedef arg argument_type;
       typedef result result_type;
};

たとえば、関数用のアダプターを C++98 でも書きたい場合は、

template <class T> struct even : unary_function <T,bool> {
  bool operator() (const T& x) const {return 0==x%2;}
};

find_if(bgn,end,even<int>()); //find even number

//adapter
template<typename adaptableFunction >
class unary_negate
{
   private:
       adaptableFunction fun_;
   public:
       typedef adaptableFunction::argument_type argument_type;

       typedef adaptableFunction::result_type result_type;  
       unary_negate(const adaptableFunction &f):fun_(f){}

       bool operator()(const argument_type&x) 
       {
           return !fun(x);
       }
}

find_if(bgn,end, unary_negate< even<int> >(even<int>()) ); //find odd number

なしでC++ 11でこれをどのように改善できunary_functionますか?

4

2 に答える 2

21

可変個引数テンプレートを使用すると、多くの一般的な関数構成をより簡単かつ一貫して表現できるため、古いクラフトはすべて不要になります。

使用してください:

  • std::function
  • std::bind
  • std::mem_fn
  • std::result_of
  • ラムダ

使用しないでください:

  • std::unary_functionstd::binary_function
  • std::mem_fun
  • std::bind1ststd::bind2nd
于 2014-03-13T17:57:02.070 に答える