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

    1. <small id='Dbg1U'></small><noframes id='Dbg1U'>

        何时在 lambda 上使用函子

        When to use functors over lambdas(何时在 lambda 上使用函子)

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

          1. <tfoot id='RJxcV'></tfoot>

              <bdo id='RJxcV'></bdo><ul id='RJxcV'></ul>
              <legend id='RJxcV'><style id='RJxcV'><dir id='RJxcV'><q id='RJxcV'></q></dir></style></legend>
                <tbody id='RJxcV'></tbody>

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

                • 本文介绍了何时在 lambda 上使用函子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否存在创建函子比使用 lambda 更有意义的情况?

                  Is there ever a situation where it makes more sense to create a functor than to use a lambda?

                  我知道我的问题实际上是 何时在函子上使用 lambda,但我想不出在实践中函子会首选而不是 lambda 的情况.对此有什么想法吗?

                  I know my question is effectively the reverse of when to use a lambda over a functor, but I can't think of a situation in practice where a functor would be preferred over a lambda. Any thoughts on this?

                  推荐答案

                  lambda 是一个函子 - 只是用更短的语法定义.

                  A lambda is a functor - just defined with a shorter syntax.

                  问题是这种语法是有限的.它并不总是允许您以最有效和最灵活的方式解决问题 - 或者根本不解决.在 C++14 之前,operator() 甚至不能成为模板.

                  The problem is that this syntax is limited. It doesn't always allow you to solve a problem in the most efficient and flexible way - or at all. Until C++14, the operator() couldn't even be a template.

                  此外,一个 lambda 表达式只有一个 operator().你不能提供几个重载来区分参数的类型:

                  Furthermore, a lambda has exactly one operator(). You can't provide several overloads to distinguish between, say, the types of the arguments:

                  struct MyComparator
                  {
                      bool operator()( int a, int b ) const {return a < b;}
                      bool operator()( float a, float b ) const {return /*Some maths here*/;}
                  };
                  

                  .. 或对象参数的值类别(即被调用的闭包对象).你也不能定义特殊的成员函数,包括构造函数和析构函数——如果一个函子负责资源怎么办?

                  .. or value category of the object argument (that is, the closure object that is called). You can also not define special member functions, including constructors and destructors - what if a functor shall be responsible for resources?

                  lambda 的另一个问题是它们不能递归.当然,普通函数(包括运算符函数)都可以.

                  Another problem with lambdas is that they cannot be recursive. Of course, normal functions (including operator functions) can be.

                  还要考虑将 lambda 用作关联容器的比较器或智能指针的删除器是不方便的:您不能直接将闭包类型作为模板参数传递,并且需要从另一个闭包对象构造容器成员.(闭包类型没有默认构造函数!).对于一个不太麻烦的块范围 map:

                  Also consider that lambdas are unhandy to use as comparators for associative containers or deleters for smart pointers: You can't directly pass the closure type as a template argument and need to construct the containers member from another closure object. (Closure types don't have a default constructor!). For a block-scope map that isn't too much of a hassle:

                  auto l = [val] (int a, int b) {return val*a < b;};
                  std::map<int, int, decltype(l)> map(l);
                  

                  现在,如果您的map数据成员,会发生什么?什么模板参数,构造函数初始化列表中的什么初始化器?您必须使用另一个静态数据成员 - 但因为您必须在类定义之外定义它,这可以说是丑陋的.

                  Now, what happens if your map is a data member? What template argument, what initializer in the constructors initialization list? You'd have to use another static data member - but since you have to define it outside the classes definition that is arguably ugly.

                  总结:Lambda 对于更复杂的场景没有用处,因为它们不是为这些场景设计的.它们提供了一种简短而简洁的方式来为相应的简单情况创建简单函数对象.

                  To sum up: Lambdas aren't useful for more complex scenarios because they weren't made for them. They provide a short and concise way of creating simple function objects for correspondingly simple situations.

                  这篇关于何时在 lambda 上使用函子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

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

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

                          <tbody id='vuv8c'></tbody>
                          <legend id='vuv8c'><style id='vuv8c'><dir id='vuv8c'><q id='vuv8c'></q></dir></style></legend><tfoot id='vuv8c'></tfoot>