【问题标题】:Why have unary_function, binary_function been removed from C++11?为什么从 C++11 中删除了 unary_function、binary_function?
【发布时间】:2014-04-18 16:11:46
【问题描述】:

我发现 binary_function 已从 C++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

如果没有unary_function,我们如何在 C++11 中改进这一点?

【问题讨论】:

    标签: c++ c++11 stl functor unary-function


    【解决方案1】:

    它没有被删除,只是在 C++11 中被弃用了。它仍然是 C++11 标准的一部分。您仍然可以在自己的代码中使用它。但它在 C++17 中已被删除。

    它不再在标准中使用,因为要求实现从binary_function 派生是过度规范。

    用户不必关心less是否派生自binary_function,他们只需要关心它定义了first_argument_type、second_argument_type和result_type。如何提供这些 typedef 应该取决于实现。

    强制实现从特定类型派生意味着用户可能开始依赖该派生,这是没有意义的,也没有用处。

    编辑

    在没有 unary_function 的情况下,我们如何在 c++11 中改进这一点?

    你不需要它。

    template<typename adaptableFunction>
    class unary_negate
    {
       private:
           adaptableFunction fun_;
       public:
           unary_negate(const adaptableFunction& f):fun_(f){}
    
           template<typename T>
               auto operator()(const T& x)  -> decltype(!fun_(x))
               {
                   return !fun_(x);
               }
    }
    

    事实上你可以做得更好,见not_fn: a generalized negator

    【讨论】:

      【解决方案2】:

      使用可变参数模板,可以更简单、更一致地表达许多通用函数组合,因此不再需要所有老套路:

      请使用:

      • std::function
      • std::bind
      • std::mem_fn
      • std::result_of
      • lambdas

      不要使用:

      • std::unary_function, std::binary_function
      • std::mem_fun
      • std::bind1st, std::bind2nd

      【讨论】:

      • binary_function 可以简化我们的工作,让代码更加一致,对吧?
      • @camino:但是可以吗?
      • 没有它我们需要定义 typedef T first_argument_type;.... ,也许我们会忘记一些东西
      • @camino:我们呢?为什么不使用现有的std::function::first_argument_type?
      • @bergercookie:什么是“FYIW”?!
      猜你喜欢
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2010-10-03
      • 2016-05-12
      • 2015-12-30
      • 2015-04-02
      相关资源
      最近更新 更多