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

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

      <bdo id='G1r8v'></bdo><ul id='G1r8v'></ul>

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

        我们可以使用 lambda 表达式作为函数参数的默认值吗?

        时间:2023-07-01

        • <tfoot id='QUqCl'></tfoot>
        • <legend id='QUqCl'><style id='QUqCl'><dir id='QUqCl'><q id='QUqCl'></q></dir></style></legend>

                <tbody id='QUqCl'></tbody>
            1. <small id='QUqCl'></small><noframes id='QUqCl'>

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

                  <bdo id='QUqCl'></bdo><ul id='QUqCl'></ul>
                • 本文介绍了我们可以使用 lambda 表达式作为函数参数的默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  参考 C++11 规范 (5.1.2.13):

                  Refering to the C++11 specification (5.1.2.13):

                  出现在默认参数中的 lambda 表达式不应隐式或显式捕获任何实体.
                  [ 示例:

                  A lambda-expression appearing in a default argument shall not implicitly or explicitly capture any entity.
                  [ Example:

                  void f2() {
                      int i = 1;
                      void g1(int = ([i]{ return i; })()); // ill-formed
                      void g2(int = ([i]{ return 0; })()); // ill-formed
                      void g3(int = ([=]{ return i; })()); // ill-formed
                      void g4(int = ([=]{ return 0; })()); // OK
                      void g5(int = ([]{ return sizeof i; })()); // OK
                  }
                  

                  ——结束示例 ]

                  但是,我们是否也可以使用 lambda 表达式本身作为函数参数的默认值?

                  However, can we also use a lambda-expression itself as the default value for a function argument?

                  例如

                  template<typename functor>
                  void foo(functor const& f = [](int x){ return x; })
                  {
                  }
                  

                  推荐答案

                  是的.在这方面,lambda 表达式与其他表达式(例如 0)没有区别.但请注意,默认参数不使用推导.换句话说,如果你声明

                  Yes. In this respect lambda expressions are no different from other expressions (like, say, 0). But note that deduction is not used with defaulted parameters. In other words, if you declare

                  template<typename T>
                  void foo(T = 0);
                  

                  then foo(0); 将调用 foofoo() 是格式错误的.您需要显式调用 foo() .由于在您的情况下您使用的是 lambda 表达式,因此没有人可以调用 foo ,因为表达式的类型(在默认参数的位置)是唯一的.但是你可以这样做:

                  then foo(0); will call foo<int> but foo() is ill-formed. You'd need to call foo<int>() explicitly. Since in your case you're using a lambda expression nobody can call foo since the type of the expression (at the site of the default parameter) is unique. However you can do:

                  // perhaps hide in a detail namespace or some such
                  auto default_parameter = [](int x) { return x; };
                  
                  template<
                      typename Functor = decltype(default_parameter)
                  >
                  void foo(Functor f = default_parameter);
                  

                  这篇关于我们可以使用 lambda 表达式作为函数参数的默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将不可复制的闭包对象传递给 std::function 参数 下一篇:班级在 Vim 中高亮显示函数名称

                  相关文章

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

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

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

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

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