• <small id='RO0ti'></small><noframes id='RO0ti'>

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

    <tfoot id='RO0ti'></tfoot>

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

        可以表达 lambda 表达式的“类型"吗?

        时间:2023-06-30
          <tbody id='z0A7I'></tbody>

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

        <tfoot id='z0A7I'></tfoot>

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

              • <bdo id='z0A7I'></bdo><ul id='z0A7I'></ul>
                1. <legend id='z0A7I'><style id='z0A7I'><dir id='z0A7I'><q id='z0A7I'></q></dir></style></legend>
                  本文介绍了可以表达 lambda 表达式的“类型"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  将 lambda 表达式视为可调用对象的语法糖",是否可以表达未命名的基础类型?

                  Thinking of lambda expressions as 'syntactic sugar' for callable objects, can the unnamed underlying type be expressed?

                  一个例子:

                  struct gt {
                      bool operator() (int l, int r) {
                          return l > r;
                      }
                  } ;
                  

                  现在,[](int l, int r) { return l >r;} 是对上述代码的优雅替代(加上必要的 gt 可调用对象的创建),但是有没有办法表达 gt(类型)本身?

                  Now, [](int l, int r) { return l > r; } is an elegant replacement for the above code (plus the necessary creation of callable objects of gt), but is there a way to express gt (the type) itself?

                  一个简单的用法:

                  std::set<int, gt> s1;  // A reversed-order std::set
                  // Is there a way to do the same using a lambda?
                  std::set<int, some-magic-here-maybe([](int l, int r) { return l > r; }) > s2;
                  

                  推荐答案

                  不,你不能把它放到 decltype 因为

                  No, you cannot put it into decltype because

                  lambda 表达式不应出现在未计算的操作数中

                  A lambda-expression shall not appear in an unevaluated operand

                  您可以执行以下操作

                  auto n = [](int l, int r) { return l > r; };
                  std::set<int, decltype(n)> s(n);
                  

                  但这真的很丑.请注意,每个 lambda 表达式都会创建一个新的唯一类型.如果之后您在其他地方执行以下操作,ts

                  But that is really ugly. Note that each lambda expression creates a new unique type. If afterwards you do the following somewhere else, t has a different type than s

                  auto n = [](int l, int r) { return l > r; };
                  std::set<int, decltype(n)> t(n);
                  

                  您可以在此处使用 std::function,但请注意,这会产生一点运行时成本,因为它需要间接调用 lambda 函数对象调用运算符.在这里它可能可以忽略不计,但如果你想以这种方式将函数对象传递给 std::sort 可能很重要.

                  You can use std::function here, but note that this will incur a tiny bit of runtime cost because it needs an indirect call to the lambda function object call operator. It's probably negligible here, but may be significant if you want to pass function objects this way to std::sort for example.

                  std::set<int, function<bool(int, int)>> s([](int l, int r) { return l > r; });
                  

                  和往常一样,先编写代码,然后配置文件 :)

                  As always, first code then profile :)

                  这篇关于可以表达 lambda 表达式的“类型"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ lambda 表达式的生命周期是多少? 下一篇:Lambda 捕获和具有相同名称的参数 - 谁在影响另一个?(clang vs gcc)

                  相关文章

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

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

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

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