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

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

      1. 从 lambda 构造 std::function 参数

        Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
      2. <tfoot id='inPZa'></tfoot>

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

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

                <legend id='inPZa'><style id='inPZa'><dir id='inPZa'><q id='inPZa'></q></dir></style></legend>
                  <tbody id='inPZa'></tbody>
                1. 本文介绍了从 lambda 构造 std::function 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下模板化函数(编译器中启用了 C++ 最新标准 - 但也许 17 个就足够了).

                  #include 模板void MyFunction(const std::function& callback);int main(){MyFunction(std::function([](int){}));MyFunction([](int){});}

                  第一个调用编译,当我将它显式转换为 std::function 时,但第二种情况没有.

                  在第一种情况下,模板推导是自动完成的,编译器只知道它将其转换为某个 std::function 并能够推导参数和返回类型.

                  然而,在第二种情况下,它应该(?)也知道 lambda 应该被转换为一些 std::function,但仍然无法做到.

                  有没有办法让第二个运行?或者是因为模板根本不进行自动转换?

                  错误信息是:

                  <块引用>

                  错误 C2672:'MyFunction':找不到匹配的重载函数

                  错误 C2784:'void MyFunction(const std::function<_Ret(_Types...)> &)':无法推导出 'const std::function<_Ret(_Types...) 的模板参数>

                  注意:见'MyFunction'的声明

                  我的目标是python 风格的装饰器".所以基本上是这样的:

                  templateauto MyFunction(std::function&& callback) ->std::function{return [callback = std::move(callback)](TArgs...args)->TReturn{返回回调(std::forward(args)...);};}

                  如果我使用模板而不是 std::function,我将如何推导出参数包和返回值?有没有办法通过一些可调用特征"从可调用对象中获取它?

                  解决方案

                  或者是因为模板根本不进行自动转换?

                  是的.模板参数推导不会考虑隐式转换.><块引用>

                  类型推导不考虑隐式转换(上面列出的类型调整除外):这是重载解析的工作,稍后发生.

                  这意味着给定 MyFunction([](int){});,隐式转换(从 lambda 到 std::function)不会被考虑,然后对 TReturnTArgs 的推导失败,调用尝试也失败.

                  作为解决方法,您可以

                  1. 使用显示的显式转换
                  2. 正如 评论所建议的那样,只需使用函子的单个模板参数.例如

                    templateauto MyFunction2(F&&回调){return [callback = std::move(callback)](auto&&... args){return callback(std::forward(args)...);};}

                  I have the following templated function (C++ latest standard is enabled in the compiler - but maybe 17 would be enough).

                  #include <functional>
                  
                  template<typename TReturn, typename ...TArgs>
                  void MyFunction(const std::function<TReturn(TArgs...)>& callback);
                  
                  int main()
                  {
                      MyFunction(std::function([](int){}));
                      MyFunction([](int){});
                  }
                  

                  The first call compiles, when I explicitly convert it to std::function, but the second case does not.

                  In the first case the template deduction is done automatically, the compiler only knows that it shall convert it to some std::function and able to deduce the parameter and return type.

                  However in the second case it shall(?) also know that the lambda shall be converted to some std::function, but still unable to do it.

                  Is there a solution to get the second one running? Or can it be that for templates the automatic conversion does not take place at all?

                  The error message is:

                  error C2672: 'MyFunction': no matching overloaded function found

                  error C2784: 'void MyFunction(const std::function<_Ret(_Types...)> &)': could not deduce template argument for 'const std::function<_Ret(_Types...)>

                  note: see declaration of 'MyFunction'

                  What I am aiming for is a "python style decorator". So basically this:

                  template<typename TReturn, typename ...TArgs>
                  auto MyFunction(std::function<TReturn(TArgs...)>&& callback) -> std::function<TReturn(TArgs...)>
                  {
                       return [callback = std::move(callback)](TArgs... args)->TReturn
                       {
                            return callback(std::forward<TArgs>(args)...);
                      };
                  }
                  

                  If I used a template instead of std::function, the how would I deduce the parameter pack and return value? Is there some way to get it from a callable via some "callable traits"?

                  解决方案

                  Or can it be that for templates the automatic conversion does not take place at all?

                  Yes. Implicit conversions won't be considered in template argument deduction.

                  Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.

                  That means given MyFunction([](int){});, the implicit conversion (from lambda to std::function) won't be considered, then the deduction for TReturn and TArgs fails and the invocation attempt fails too.

                  As the workarounds, you can

                  1. Use explicit conversion as you showed
                  2. As the comment suggested, just use a single template parameter for functors. e.g.

                    template<typename F>
                    auto MyFunction2(F&& callback)
                    {
                         return [callback = std::move(callback)](auto&&... args)
                         {
                              return callback(std::forward<decltype(args)>(args)...);
                         };
                    }
                    

                  这篇关于从 lambda 构造 std::function 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)
                2. <legend id='zZCqu'><style id='zZCqu'><dir id='zZCqu'><q id='zZCqu'></q></dir></style></legend>

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

                      <tfoot id='zZCqu'></tfoot>
                        <tbody id='zZCqu'></tbody>

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

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