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

    <tfoot id='XUkCK'></tfoot>

        <bdo id='XUkCK'></bdo><ul id='XUkCK'></ul>
        <legend id='XUkCK'><style id='XUkCK'><dir id='XUkCK'><q id='XUkCK'></q></dir></style></legend>
      1. 为什么在 C++14 中使用 std::bind 而不是 lambdas?

        Why use std::bind over lambdas in C++14?(为什么在 C++14 中使用 std::bind 而不是 lambdas?)
        <i id='UI7Ra'><tr id='UI7Ra'><dt id='UI7Ra'><q id='UI7Ra'><span id='UI7Ra'><b id='UI7Ra'><form id='UI7Ra'><ins id='UI7Ra'></ins><ul id='UI7Ra'></ul><sub id='UI7Ra'></sub></form><legend id='UI7Ra'></legend><bdo id='UI7Ra'><pre id='UI7Ra'><center id='UI7Ra'></center></pre></bdo></b><th id='UI7Ra'></th></span></q></dt></tr></i><div id='UI7Ra'><tfoot id='UI7Ra'></tfoot><dl id='UI7Ra'><fieldset id='UI7Ra'></fieldset></dl></div>
              • <bdo id='UI7Ra'></bdo><ul id='UI7Ra'></ul>

                  <tbody id='UI7Ra'></tbody>

                1. <legend id='UI7Ra'><style id='UI7Ra'><dir id='UI7Ra'><q id='UI7Ra'></q></dir></style></legend>

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

                2. <tfoot id='UI7Ra'></tfoot>
                  本文介绍了为什么在 C++14 中使用 std::bind 而不是 lambdas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C++11 之前,我经常使用 boost::bindboost::lambda.bind 部分成为标准库(std::bind),另一部分成为核心语言(C++ lambdas)的一部分,并大量使用 lambdas更轻松.现在,我几乎不使用 std::bind,因为我几乎可以用 C++ lambda 做任何事情.我能想到的 std::bind 有一个有效的用例:

                  Before C++11 I used boost::bind or boost::lambda a lot. The bind part made it into the standard library (std::bind) the other part became part of the core language (C++ lambdas) and made the use of lambdas a lot easier. Nowadays, I hardly use std::bind, since I can do almost anything with C++ lambdas. There's one valid use-case for std::bind that I can think of:

                  struct foo
                  {
                    template < typename A, typename B >
                    void operator()(A a, B b)
                    {
                      cout << a << ' ' << b;
                    }
                  };
                  
                  auto f = bind(foo(), _1, _2);
                  f( "test", 1.2f ); // will print "test 1.2"
                  

                  C++14 的等价物是

                  The C++14 equivalent for that would be

                  auto f = []( auto a, auto b ){ cout << a << ' ' << b; }
                  f( "test", 1.2f ); // will print "test 1.2"
                  

                  更短更简洁.(在 C++11 中,由于自动参数,这还不起作用.)是否有任何其他有效的 std::bind 用例击败 C++ lambdas 替代方案,或者是 std::bind 用 C++14 是多余的吗?

                  Much shorter and more concise. (In C++11 this does not work yet because of the auto parameters.) Is there any other valid use case for std::bind beating the C++ lambdas alternative or is std::bind superfluous with C++14?

                  推荐答案

                  Scott Meyers 给出了一个 谈论关于这个.这是我记得的:

                  Scott Meyers gave a talk about this. This is what I remember:

                  在 C++14 中,没有什么有用的 bind 可以做而 lambdas 也做不到.

                  In C++14 there is nothing useful bind can do that can't also be done with lambdas.

                  在 C++11 中,但是有些事情不能用 lambda 来完成:

                  In C++11 however there are some things that can't be done with lambdas:

                  1. 在创建 lambda 表达式时,您不能在捕获时移动变量.变量总是被捕获为左值.对于绑定,您可以编写:

                  1. You can't move the variables while capturing when creating the lambdas. Variables are always captured as lvalues. For bind you can write:

                  auto f1 = std::bind(f, 42, _1, std::move(v));
                  

                3. 无法捕获表达式,只能捕获标识符.对于绑定,您可以编写:

                4. Expressions can't be captured, only identifiers can. For bind you can write:

                  auto f1 = std::bind(f, 42, _1, a + b);
                  

                5. 重载函数对象的参数.问题中已经提到了这一点.

                6. Overloading arguments for function objects. This was already mentioned in the question.

                  在 C++14 中,所有这些都是可能的.

                  In C++14 all of these possible.

                  1. 移动示例:

                  1. Move example:

                  auto f1 = [v = std::move(v)](auto arg) { f(42, arg, std::move(v)); };
                  

                7. 表达式示例:

                8. Expression example:

                  auto f1 = [sum = a + b](auto arg) { f(42, arg, sum); };
                  

                9. 查看问题

                10. See question

                  完美转发:可以写

                  auto f1 = [=](auto&& arg) { f(42, std::forward<decltype(arg)>(arg)); };
                  

                11. 绑定的一些缺点:

                  • 绑定按名称绑定,因此如果您有多个具有相同名称的函数(重载函数),则绑定不知道该使用哪个.下面的例子不会编译,而 lambdas 不会有问题:

                  • Bind binds by name and as a result if you have multiple functions with the same name (overloaded functions) bind doesn't know which one to use. The following example won't compile, while lambdas wouldn't have a problem with it:

                  void f(int); void f(char); auto f1 = std::bind(f, _1, 42);
                  

                12. 使用绑定函数时不太可能被内联
                13. 另一方面,lambdas 理论上可能生成比绑定更多的模板代码.因为对于每个 lambda,您都会获得一个独特的类型.对于绑定来说,只有当你有不同的参数类型和不同的函数时(我猜在实践中,你用相同的参数和函数绑定多次的情况并不经常发生).

                  On the other hand lambdas might theoretically generate more template code than bind. Since for each lambda you get a unique type. For bind it is only when you have different argument types and a different function (I guess that in practice however it doesn't happen very often that you bind several time with the same arguments and function).

                  Jonathan Wakely 在他的回答中提到的实际上是不使用 bind 的另一个原因.我不明白你为什么要默默地忽略争论.

                  What Jonathan Wakely mentioned in his answer is actually one more reason not to use bind. I can't see why you would want to silently ignore arguments.

                  这篇关于为什么在 C++14 中使用 std::bind 而不是 lambdas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++程序中的堆栈溢出错误)
                14. <small id='rmRsa'></small><noframes id='rmRsa'>

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

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

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