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

    • <bdo id='9udlu'></bdo><ul id='9udlu'></ul>

        <tfoot id='9udlu'></tfoot>

        <small id='9udlu'></small><noframes id='9udlu'>

        <legend id='9udlu'><style id='9udlu'><dir id='9udlu'><q id='9udlu'></q></dir></style></legend>

        <i id='9udlu'><tr id='9udlu'><dt id='9udlu'><q id='9udlu'><span id='9udlu'><b id='9udlu'><form id='9udlu'><ins id='9udlu'></ins><ul id='9udlu'></ul><sub id='9udlu'></sub></form><legend id='9udlu'></legend><bdo id='9udlu'><pre id='9udlu'><center id='9udlu'></center></pre></bdo></b><th id='9udlu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9udlu'><tfoot id='9udlu'></tfoot><dl id='9udlu'><fieldset id='9udlu'></fieldset></dl></div>
      1. 我們可以獲得 lambda 參數的類型嗎?

        Can we get the type of a lambda argument?(我們可以獲得 lambda 參數的類型嗎?)
        <legend id='795Nf'><style id='795Nf'><dir id='795Nf'><q id='795Nf'></q></dir></style></legend>
        <tfoot id='795Nf'></tfoot>

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

              <small id='795Nf'></small><noframes id='795Nf'>

                  <bdo id='795Nf'></bdo><ul id='795Nf'></ul>
                    <tbody id='795Nf'></tbody>
                  本文介紹了我們可以獲得 lambda 參數的類型嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  使用 std::function,我們可以使用 argument_typesecond_argument_type 等 typedef 來獲取參數的類型,但我可以看不到用 lambda 做同樣事情的方法.是否可以?(我使用的是 VS2010)

                  Using std::function, we can get the type of an argument using the argument_type, second_argument_type etc. typedefs, but I can't see a way to do the same thing with lambdas. Is it possible? (I'm using VS2010)

                  假設我想要在用于讀取對象并將其傳遞給 setter 函數的反序列化系統中使用以下內容:

                  Say I want something like the following in my deserialization system used to read an object and pass it to a setter function:

                  template<typename F> 
                  static void forward(F f)
                  { 
                      // Create an object of the type of the first
                      // parameter to the function object F
                      typedef typename F::argument_type T;
                      T t;
                  
                      //...do something with 't' here (deserialize in my case)
                  
                      // Forward the object to the function
                      f(t);
                  }
                  

                  它可以像這樣使用并且一切正常:

                  It can be used like this and everything works fine:

                  std::function<void(int)> f = [](int i) -> void { setValue(i); };
                  forward(f);
                  

                  但它不能直接與 lambdas 一起使用:

                  But it will not work directly with lambdas:

                  forward([](int i) -> void { setValue(i); });
                  //error C2039: 'argument_type' : is not a 
                  //member of '`anonymous-namespace'::<lambda1>'
                  

                  有沒有一種方法可以以同時適用于 lambda 和 std::function 對象的方式訪問參數類型?也許是一種先獲取 lambda 的 std::function 類型,然后從中獲取 argument_type 的方法?

                  Is there a way to access the parameter types in a way that will work for both lambdas and std::function objects? Maybe a way to get the std::function type of a lambda first, and then the argument_type from that?

                  根據下面的答案,適用于 lambdas 和 std::function 的版本是:

                  Following on from the answer below, a version that works with lambdas and std::function is:

                  template<typename T, typename F> 
                  static void forward(F f)
                  { 
                      T t;
                  
                      //...do something with 't' here (deserialize in my case)
                  
                      f(t);
                  }
                  
                  forward<int>([](int i) -> void { setValue(i); });
                  

                  由于 int 在這里重復,我希望擺脫它 - 對于 int 來說還不錯,但對于幾個命名空間中的長命名類型來說更煩人.C'est la vie!

                  Since int is repeated here I was hoping to get rid of it - not so bad for int but more annoying for long-named types in a couple of namespaces. C'est la vie!

                  推薦答案

                  在一般情況下是不可取的.(請注意,std::function 很容易指定例如 argument_type 是什么:它只是 A!它在類型定義中可用.)

                  It's not desirable in the general case. (Note that it's quite easy for std::function<T(A)> to specify what e.g. argument_type is: it's just A! It's available in the type definition.)

                  可以要求每個函數對象類型指定其參數類型,然后強制要求從 lambda 表達式生成的閉包類型這樣做.事實上,像自適應函子這樣的 C++0x 之前的特性只適用于這種類型.

                  It would be possible to require each and every function object type to specify its argument types, and in turn mandate that the closure types generated from lambda expression do so. In fact, pre-C++0x features like adaptable functors would only work for such types.

                  然而,我們正在使用 C++0x 并且有充分的理由.最簡單的就是簡單的重載:一個帶有模板化 operator() 的函子類型(又名多態函子)簡單地接受所有類型的參數;那么 argument_type 應該是什么?另一個原因是泛型代碼(通常)試圖對其操作的類型和對象指定最少的約束,以便更容易(重新)使用.

                  However, we're moving from that with C++0x and for good reasons. The simplest of which is simply overloading: a functor type with a templated operator() (a.k.a a polymorphic functor) simply takes all kind of arguments; so what should argument_type be? Another reason is that generic code (usually) attempts to specify the least constraints on the types and objects it operates on in order to more easily be (re)used.

                  換句話說,對于給定的 Functor f, typename Functor::argumentint.知道 f(0) 是一個可接受的表達式,更有趣.為此,C++0x 提供了諸如 decltypestd::declval 之類的工具(方便地將兩者打包在 std::result_of 中).

                  In other words, generic code is not really interested that given Functor f, typename Functor::argument be int. It's much more interesting to know that f(0) is an acceptable expression. For this C++0x gives tools such as decltype and std::declval (conveniently packaging the two inside std::result_of).

                  在我看來,您有兩種選擇:要求傳遞給模板的所有函子使用 C++03 風格的約定,指定 argument_type 等;使用以下技術;或重新設計.我建議使用最后一個選項,但這是您的決定,因為我不知道您的代碼庫是什么樣的或您的要求是什么.

                  The way I see it you have two choices: require that all functors passed to your template use a C++03-style convention of specifying an argument_type and the like; use the technique below; or redesign. I'd recommend the last option but it's your call since I don't know what your codebase looks like or what your requirements are.

                  對于單態函子類型(即沒有重載),可以檢查 operator() 成員.這適用于 lambda 表達式的閉包類型.

                  For a monomorphic functor type (i.e. no overloading), it is possible to inspect the operator() member. This works for the closure types of lambda expressions.

                  所以我們聲明這些助手

                  template<typename F, typename Ret, typename A, typename... Rest>
                  A
                  helper(Ret (F::*)(A, Rest...));
                  
                  template<typename F, typename Ret, typename A, typename... Rest>
                  A
                  helper(Ret (F::*)(A, Rest...) const);
                  
                  // volatile or lvalue/rvalue *this not required for lambdas (phew)
                  

                  接受一個指向至少一個參數的成員函數的指針.現在:

                  that accept a pointer to member function taking at least one argument. And now:

                  template<typename F>
                  struct first_argument {
                      typedef decltype( helper(&F::operator()) ) type;
                  };
                  

                  [一個精心設計的特征可以連續查詢左值-右值/常量/易失性重載,如果第一個參數對所有重載都相同,則公開第一個參數,或者使用 std::common_type.]

                  [ an elaborate trait could successively query the lvalue-rvalue/const/volatile overloads and expose the first argument if it's the same for all overloads, or use std::common_type.]

                  這篇關于我們可以獲得 lambda 參數的類型嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)
                  <legend id='H1q1S'><style id='H1q1S'><dir id='H1q1S'><q id='H1q1S'></q></dir></style></legend>

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

                          <tbody id='H1q1S'></tbody>
                          <bdo id='H1q1S'></bdo><ul id='H1q1S'></ul>

                          <i id='H1q1S'><tr id='H1q1S'><dt id='H1q1S'><q id='H1q1S'><span id='H1q1S'><b id='H1q1S'><form id='H1q1S'><ins id='H1q1S'></ins><ul id='H1q1S'></ul><sub id='H1q1S'></sub></form><legend id='H1q1S'></legend><bdo id='H1q1S'><pre id='H1q1S'><center id='H1q1S'></center></pre></bdo></b><th id='H1q1S'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='H1q1S'><tfoot id='H1q1S'></tfoot><dl id='H1q1S'><fieldset id='H1q1S'></fieldset></dl></div>
                            <tfoot id='H1q1S'></tfoot>
                          1. 主站蜘蛛池模板: 一区二区av| 精品国产一区二区在线 | 美女视频黄色片 | 亚洲国产成人av好男人在线观看 | 亚洲精品视频久久 | 国产成年人小视频 | 中文字幕精品一区二区三区精品 | av网站在线播放 | 国产一区二区三区在线 | 黄色一级视频免费 | 香蕉超碰| 亚洲午夜视频 | 日韩激情免费 | 一级做a爰片性色毛片视频停止 | 国产成人精品久久二区二区91 | 男女羞羞免费网站 | 国产污视频在线 | 神马九九 | 亚洲福利一区 | 日本一区二区三区四区 | 四虎影 | 亚洲人人 | 日韩三级一区 | 成人在线免费视频 | 亚洲一区日韩 | 亚洲精品日韩一区二区电影 | 精品欧美乱码久久久久久1区2区 | 中文字幕视频在线观看 | 免费国产视频在线观看 | 好姑娘高清在线观看电影 | 精品99在线 | 福利国产 | 妖精视频一区二区三区 | 色婷婷综合久久久中字幕精品久久 | 亚洲综合国产 | 国产一区二区三区四区三区四 | 亚洲人成人一区二区在线观看 | 精品九九久久 | 亚洲成人精品视频 | 日韩在线观看一区二区三区 | 国产精品午夜电影 |