• <bdo id='sFVRh'></bdo><ul id='sFVRh'></ul>

  • <tfoot id='sFVRh'></tfoot>
    1. <i id='sFVRh'><tr id='sFVRh'><dt id='sFVRh'><q id='sFVRh'><span id='sFVRh'><b id='sFVRh'><form id='sFVRh'><ins id='sFVRh'></ins><ul id='sFVRh'></ul><sub id='sFVRh'></sub></form><legend id='sFVRh'></legend><bdo id='sFVRh'><pre id='sFVRh'><center id='sFVRh'></center></pre></bdo></b><th id='sFVRh'></th></span></q></dt></tr></i><div id='sFVRh'><tfoot id='sFVRh'></tfoot><dl id='sFVRh'><fieldset id='sFVRh'></fieldset></dl></div>
      <legend id='sFVRh'><style id='sFVRh'><dir id='sFVRh'><q id='sFVRh'></q></dir></style></legend>

        <small id='sFVRh'></small><noframes id='sFVRh'>

        为什么从 C++11 中删除了 unary_function、binary_function?

        时间:2024-05-12
          <bdo id='lSYgt'></bdo><ul id='lSYgt'></ul>

            <small id='lSYgt'></small><noframes id='lSYgt'>

              <tbody id='lSYgt'></tbody>

          • <tfoot id='lSYgt'></tfoot>

              1. <i id='lSYgt'><tr id='lSYgt'><dt id='lSYgt'><q id='lSYgt'><span id='lSYgt'><b id='lSYgt'><form id='lSYgt'><ins id='lSYgt'></ins><ul id='lSYgt'></ul><sub id='lSYgt'></sub></form><legend id='lSYgt'></legend><bdo id='lSYgt'><pre id='lSYgt'><center id='lSYgt'></center></pre></bdo></b><th id='lSYgt'></th></span></q></dt></tr></i><div id='lSYgt'><tfoot id='lSYgt'></tfoot><dl id='lSYgt'><fieldset id='lSYgt'></fieldset></dl></div>

                  <legend id='lSYgt'><style id='lSYgt'><dir id='lSYgt'><q id='lSYgt'></q></dir></style></legend>

                  本文介绍了为什么从 C++11 中删除了 unary_function、binary_function?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我发现 binary_function 已从 C++11 中删除.我想知道为什么.

                  I found that binary_function is removed from C++11. I am wondering why.

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

                  已修改---------------------------------------------------------------------------

                  MODIFIED----------------------------------------------------------------------------

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

                  例如,如果我们想在 C++98 中编写函数适配器,

                  For example, if we want to write our adapter for function even in 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 中改进这一点?

                  How can we improve this in C++11 without unary_function?

                  推荐答案

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

                  It isn't removed, it's just deprecated in C++11. It's still part of the C++11 standard. You can still use it in your own code. It was removed in C++17 though.

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

                  It isn't used in the standard any more because requiring implementations to derive from binary_function is over-specification.

                  用户不应该关心less是否从binary_function派生,他们只需要关心它定义了first_argument_typesecond_argument_typeresult_type.如何提供这些 typedef 应该取决于实现.

                  Users should not care whether less derives from binary_function, they only need to care that it defines first_argument_type, second_argument_type and result_type. It should be up to the implementation how it provides those typedefs.

                  强制实现从特定类型派生意味着用户可能开始依赖该派生,这毫无意义且无用.

                  Forcing the implementation to derive from a specific type means that users might start relying on that derivation, which makes no sense and is not useful.

                  编辑

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

                  How can we improve this in c++11 without unary_function?

                  你不需要它.

                  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:一个广义的否定

                  In fact you can do even better, see not_fn: a generalized negator

                  这篇关于为什么从 C++11 中删除了 unary_function、binary_function?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:STL MAP 应该使用 find() 或 [n] 标识符来查找地图中的元素吗? 下一篇:使用 std::sort 对集合进行排序

                  相关文章

                  <small id='sEc13'></small><noframes id='sEc13'>

                  • <bdo id='sEc13'></bdo><ul id='sEc13'></ul>
                    <legend id='sEc13'><style id='sEc13'><dir id='sEc13'><q id='sEc13'></q></dir></style></legend>

                      <i id='sEc13'><tr id='sEc13'><dt id='sEc13'><q id='sEc13'><span id='sEc13'><b id='sEc13'><form id='sEc13'><ins id='sEc13'></ins><ul id='sEc13'></ul><sub id='sEc13'></sub></form><legend id='sEc13'></legend><bdo id='sEc13'><pre id='sEc13'><center id='sEc13'></center></pre></bdo></b><th id='sEc13'></th></span></q></dt></tr></i><div id='sEc13'><tfoot id='sEc13'></tfoot><dl id='sEc13'><fieldset id='sEc13'></fieldset></dl></div>
                    1. <tfoot id='sEc13'></tfoot>