• <legend id='g08qq'><style id='g08qq'><dir id='g08qq'><q id='g08qq'></q></dir></style></legend>

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

    <tfoot id='g08qq'></tfoot>

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

        Lambda 和 std::function

        时间:2023-05-24
          <bdo id='3pJqp'></bdo><ul id='3pJqp'></ul>

          <small id='3pJqp'></small><noframes id='3pJqp'>

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

              <tbody id='3pJqp'></tbody>

              • <legend id='3pJqp'><style id='3pJqp'><dir id='3pJqp'><q id='3pJqp'></q></dir></style></legend>

                  本文介绍了Lambda 和 std::function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在努力赶上 C++11 和所有很棒的新功能.我有点坚持 lambdas.

                  I'm trying to catch up on C++11 and all the great new features. I'm a bit stuck on lambdas.

                  这是我能够开始工作的代码:

                  Here's the code I was able to get to work:

                  #include <iostream>
                  #include <cstdlib>
                  #include <vector>
                  #include <string>
                  #include <functional>
                  
                  using namespace std;
                  
                  template<typename BaseT, typename Func>
                  vector<BaseT> findMatches(vector<BaseT> search, Func func)
                  {
                      vector<BaseT> tmp;
                  
                      for(auto item : search)
                      {
                          if( func(item) )
                          {
                              tmp.push_back(item);
                          }
                      }
                  
                      return tmp;
                  }
                  
                  void Lambdas()
                  {
                      vector<int> testv = { 1, 2, 3, 4, 5, 6, 7 };
                  
                      auto result = findMatches(testv, [] (const int &x) { return x % 2 == 0; });
                  
                      for(auto i : result)
                      {
                          cout << i << endl;
                      }
                  }
                  
                  int main(int argc, char* argv[])
                  {
                  
                      Lambdas();
                  
                      return EXIT_SUCCESS;
                  }
                  

                  我想要的是这个:

                  template<typename BaseT>
                  vector<BaseT> findMatches(vector<BaseT> search, function <bool (const BaseT &)> func)
                  {
                      vector<BaseT> tmp;
                  
                      for(auto item : search)
                      {
                          if( func(item) )
                          {
                              tmp.push_back(item);
                          }
                      }
                  
                      return tmp;
                  }
                  

                  基本上我想将可能的 lambdas 缩小到一个合理的函数子集.我错过了什么?这甚至可能吗?我使用的是 GCC/G++ 4.6.

                  Basically I want to narrow down the possible lambdas to a sensible subset of functions. What am I missing? Is this even possible? I'm using GCC/G++ 4.6.

                  推荐答案

                  Stephan T. Lavavej 解释了为什么这在 这个视频.基本上,问题在于编译器试图从 both std::vector 推导出 BaseTcode>std::function 参数.C++ 中的 lambda 不是 std::function 类型,它是一种未命名的、唯一的非联合类型,如果它没有捕获列表(空 []).另一方面,可以从任何可能类型的可调用实体(函数指针、成员函数指针、函数对象)创建 std::function 对象.

                  Stephan T. Lavavej explains why this doesn't work in this video. Basically, the problem is that the compiler tries to deduce BaseT from both the std::vector and the std::function parameter. A lambda in C++ is not of type std::function, it's an unnamed, unique non-union type that is convertible to a function pointer if it doesn't have a capture list (empty []). On the other hand, a std::function object can be created from any possible type of callable entity (function pointers, member function pointers, function objects).

                  请注意,我个人不明白为什么您要将传入的函子限制为该特定签名(除了通过多态函数包装器间接进行的事实之外,例如 std::function, 远比直接调用函子(甚至可能被内联)低效),但这是一个工作版本.基本上,它禁用了 std::function 部分的参数推导,并且只从 std::vector 参数推导了 BaseT:

                  Note that I personally don't understand why you would want to limit the incoming functors to that specific signature (in addition to the fact that indirection through a polymorphic function wrapper, like std::function, is by far more inefficient than a direct call to a functor (which may even be inlined)), but here's a working version. Basically, it disables argument deduction on the std::function part, and only deduces BaseT from the std::vector argument:

                  template<class T>
                  struct Identity{
                    typedef T type;
                  };
                  
                  template<typename BaseT>
                  vector<BaseT> findMatches(vector<BaseT> search, 
                      typename Identity<function<bool (const BaseT &)>>::type func)
                  {
                      vector<BaseT> tmp;
                  
                      for(auto item : search)
                      {
                          if( func(item) )
                          {
                              tmp.push_back(item);
                          }
                      }
                  
                      return tmp;
                  }
                  

                  Ideone 上的实例.

                  另一种可能的方法是不直接限制函子类型,而是通过 SFINAE 间接限制:

                  Another possible way would be to not restrict the functor type directly, but indirectly through SFINAE:

                  template<class T, class F>
                  auto f(std::vector<T> v, F fun)
                      -> decltype(bool(fun(v[0])), void())
                  {
                    // ...
                  }
                  

                  Ideone 上的实例.

                  如果 fun 不接受 T& 类型的参数或者返回类型不能转换为 ,这个函数将从重载集中删除布尔., void() 使得 f 的返回类型为 void.

                  This function will be removed from the overload set if fun doesn't take an argument of type T& or if the return type is not convertible to bool. The , void() makes f's return type void.

                  这篇关于Lambda 和 std::function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                          <tbody id='ygVYw'></tbody>
                        • <small id='ygVYw'></small><noframes id='ygVYw'>