久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" id='3pJqp'><tfoot id='3pJqp'></tfoot><dl id='3pJqp'><fieldset id='3pJqp'></fieldset></dl></div>
      <tfoot id='3pJqp'></tfoot>

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

        Lambda 和 std::function

        Lambdas and std::function(Lambda 和 std::function)
      1. <legend id='g08qq'><style id='g08qq'><dir id='g08qq'><q id='g08qq'></q></dir></style></legend>

      2. <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 class="qwawimqqmiuu" id='g08qq'><tfoot id='g08qq'></tfoot><dl id='g08qq'><fieldset id='g08qq'></fieldset></dl></div>
                    <tbody id='g08qq'></tbody>
                  本文介紹了Lambda 和 std::function的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我正在努力趕上 C++11 和所有很棒的新功能.我有點(diǎn)堅(jiān)持 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;
                  }
                  

                  我想要的是這個(gè):

                  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 縮小到一個(gè)合理的函數(shù)子集.我錯(cuò)過了什么?這甚至可能嗎?我使用的是 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 解釋了為什么這在 這個(gè)視頻.基本上,問題在于編譯器試圖從 both std::vector 推導(dǎo)出 BaseTcode>std::function 參數(shù).C++ 中的 lambda 不是 std::function 類型,它是一種未命名的、唯一的非聯(lián)合類型,如果它沒有捕獲列表(空 []).另一方面,可以從任何可能類型的可調(diào)用實(shí)體(函數(shù)指針、成員函數(shù)指針、函數(shù)對象)創(chuàng)建 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).

                  請注意,我個(gè)人不明白為什么您要將傳入的函子限制為該特定簽名(除了通過多態(tài)函數(shù)包裝器間接進(jìn)行的事實(shí)之外,例如 std::function, 遠(yuǎn)比直接調(diào)用函子(甚至可能被內(nèi)聯(lián))低效),但這是一個(gè)工作版本.基本上,它禁用了 std::function 部分的參數(shù)推導(dǎo),并且只從 std::vector 參數(shù)推導(dǎo)了 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 上的實(shí)例.

                  另一種可能的方法是不直接限制函子類型,而是通過 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 上的實(shí)例.

                  如果 fun 不接受 T& 類型的參數(shù)或者返回類型不能轉(zhuǎn)換為 ,這個(gè)函數(shù)將從重載集中刪除布爾., 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.

                  這篇關(guān)于Lambda 和 std::function的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Why do two functions have the same address?(為什么兩個(gè)函數(shù)的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復(fù)制構(gòu)造的?)
                  mixing templates with polymorphism(混合模板與多態(tài)性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時(shí)候應(yīng)該使用關(guān)鍵字“typename?使用模板時(shí))
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標(biāo)準(zhǔn)庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數(shù)模板,而 clang 不能)
                    <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 class="qwawimqqmiuu" 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'>

                            主站蜘蛛池模板: 国产高清一区二区三区 | 亚洲综合区| 日韩欧美在线一区 | 色偷偷噜噜噜亚洲男人 | 亚洲一区二区日韩 | 日韩精品在线一区二区 | 日韩精品在线免费观看 | 成人精品国产一区二区4080 | 国产精品国产三级国产aⅴ浪潮 | 丁香婷婷久久久综合精品国产 | 欧美区在线| 天天插天天操 | 三级成人在线观看 | 99视频在线看 | 免费在线观看h片 | 日日碰碰| 天堂视频免费 | 国户精品久久久久久久久久久不卡 | 99在线免费视频 | 国产精品一区二区av | 欧美黄色绿像 | 北条麻妃一区二区三区在线观看 | 国产av毛片 | 欧美伊人| 国产激情综合五月久久 | 久草新在线 | 久久久久久成人 | 国产一区二区三区免费视频 | 国产亚洲精品综合一区 | 古装人性做爰av网站 | 99精品观看 | 精品国产一区二区三区久久久蜜月 | 久久久99精品免费观看 | 欧美精品一区二区三区四区五区 | 我想看一级黄色毛片 | 日韩和的一区二在线 | 国产小视频在线观看 | 色欧美综合 | 欧美日韩在线播放 | 欧美性大战久久久久久久蜜臀 | 九九av |