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

  • <small id='4e8VH'></small><noframes id='4e8VH'>

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

      <tfoot id='4e8VH'></tfoot>
          <bdo id='4e8VH'></bdo><ul id='4e8VH'></ul>
        <legend id='4e8VH'><style id='4e8VH'><dir id='4e8VH'><q id='4e8VH'></q></dir></style></legend>

        消除作為模板參數(shù)傳遞的重載成員函數(shù)指針的歧

        Disambiguate overloaded member function pointer being passed as template parameter(消除作為模板參數(shù)傳遞的重載成員函數(shù)指針的歧義)

        <tfoot id='0Oybi'></tfoot>

            • <bdo id='0Oybi'></bdo><ul id='0Oybi'></ul>

                <i id='0Oybi'><tr id='0Oybi'><dt id='0Oybi'><q id='0Oybi'><span id='0Oybi'><b id='0Oybi'><form id='0Oybi'><ins id='0Oybi'></ins><ul id='0Oybi'></ul><sub id='0Oybi'></sub></form><legend id='0Oybi'></legend><bdo id='0Oybi'><pre id='0Oybi'><center id='0Oybi'></center></pre></bdo></b><th id='0Oybi'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0Oybi'><tfoot id='0Oybi'></tfoot><dl id='0Oybi'><fieldset id='0Oybi'></fieldset></dl></div>
                  <tbody id='0Oybi'></tbody>
              • <legend id='0Oybi'><style id='0Oybi'><dir id='0Oybi'><q id='0Oybi'></q></dir></style></legend>

                1. <small id='0Oybi'></small><noframes id='0Oybi'>

                  本文介紹了消除作為模板參數(shù)傳遞的重載成員函數(shù)指針的歧義的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試重新創(chuàng)建觀察者模式,我可以完美地將參數(shù)轉(zhuǎn)發(fā)給觀察者的給定成員函數(shù).

                  I am attempting to recreate the Observer pattern where I can perfectly forward parameters to a given member function of the observers.

                  如果我嘗試傳遞具有多重覆蓋成員函數(shù)的地址,它無法根據(jù)參數(shù)推斷出正確的成員函數(shù).

                  If I attempt to pass the address of a member function which has multiple overrides, it cannot deduce the correct member function based on the arguments.

                  #include <iostream>
                  #include <vector>
                  #include <algorithm>
                  
                  template<typename Class>
                  struct observer_list
                  {
                      template<typename Ret, typename... Args, typename... UArgs>
                      void call(Ret (Class::*func)(Args...), UArgs&&... args)
                      {
                          for (auto obj : _observers)
                          {
                              (obj->*func)(std::forward<UArgs>(args)...);
                          }
                      }
                      std::vector<Class*> _observers;
                  };
                  
                  struct foo
                  {
                      void func(const std::string& s)
                      {
                          std::cout << this << ": " << s << std::endl;
                      }
                      void func(const double d)
                      {
                          std::cout << this << ": " << d << std::endl;
                      }
                  };
                  
                  int main()
                  {
                      observer_list<foo> l;
                      foo f1, f2;
                      l._observers = { &f1, &f2 };
                  
                      l.call(&foo::func, "hello");
                      l.call(&foo::func, 0.5);
                  
                      return 0;
                  }
                  

                  這無法編譯,模板參數(shù)推導(dǎo)/替換失敗.

                  請注意,我有 Args...UArgs... 因為我需要能夠傳遞不一定與類型相同的參數(shù)函數(shù)簽名,但可以轉(zhuǎn)換為所述類型.

                  Note that I had Args... and UArgs... because I need to be able to pass parameters which are not necessarily the same type asthe type of the function signature, but are convertible to said type.

                  我想我可以使用 std::enable_if<std::is_convertible<Args, UArgs>> 調(diào)用來消除歧義,但我不相信我可以用可變參數(shù)來做到這一點模板參數(shù)包?

                  I was thinking I could use a std::enable_if<std::is_convertible<Args, UArgs>> call to disambiguate, but I don't believe I can do this with a variadic template parameter pack?

                  我怎樣才能讓模板參數(shù)推導(dǎo)在這里起作用?

                  推薦答案

                  問題在這里:

                  l.call(&foo::func, "hello");
                  l.call(&foo::func, 0.5);
                  

                  對于這兩行,編譯器不知道您指的是哪個 foo::func.因此,您必須通過強制轉(zhuǎn)換提供缺少的類型信息(即 foo:func 的類型)來消除歧義:

                  For both lines, the compiler doesn't know which foo::func you are referring to. Hence, you have to disambiguate yourself by providing the type information that is missing (i.e., the type of foo:func) through casts:

                  l.call(static_cast<void (foo::*)(const std::string&)>(&foo::func), "hello");
                  l.call(static_cast<void (foo::*)(const double      )>(&foo::func), 0.5);
                  

                  或者,您可以提供編譯器無法推導(dǎo)出的模板參數(shù),這些參數(shù)定義了 func 的類型:

                  Alternatively, you can provide the template arguments that the compiler cannot deduce and that define the type of func:

                  l.call<void, const std::string&>(&foo::func, "hello");
                  l.call<void, double            >(&foo::func, 0.5);
                  

                  請注意,您必須使用 double 而不是上面的 const double.原因是一般doubleconst double是兩種不同的類型.然而,在一種情況下 doubleconst double 被認(rèn)為是相同的類型:作為函數(shù)參數(shù).例如,

                  Notice that you have to use double and not const double above. The reason is that generally double and const double are two different types. However, there's one situation where double and const double are considered as if they were the same type: as function arguments. For instance,

                  void bar(const double);
                  void bar(double);
                  

                  不是兩個不同的重載而是實際上是同一個函數(shù).

                  are not two different overloads but are actually the same function.

                  這篇關(guān)于消除作為模板參數(shù)傳遞的重載成員函數(shù)指針的歧義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(為什么兩個函數(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(我什么時候應(yīng)該使用關(guān)鍵字“typename?使用模板時)
                  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='9NWSx'></tfoot>

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

                  <i id='9NWSx'><tr id='9NWSx'><dt id='9NWSx'><q id='9NWSx'><span id='9NWSx'><b id='9NWSx'><form id='9NWSx'><ins id='9NWSx'></ins><ul id='9NWSx'></ul><sub id='9NWSx'></sub></form><legend id='9NWSx'></legend><bdo id='9NWSx'><pre id='9NWSx'><center id='9NWSx'></center></pre></bdo></b><th id='9NWSx'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9NWSx'><tfoot id='9NWSx'></tfoot><dl id='9NWSx'><fieldset id='9NWSx'></fieldset></dl></div>
                        <tbody id='9NWSx'></tbody>
                    • <legend id='9NWSx'><style id='9NWSx'><dir id='9NWSx'><q id='9NWSx'></q></dir></style></legend>

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

                          • 主站蜘蛛池模板: 日日操日日干 | 成人免费毛片男人用品 | 亚洲第一毛片 | 黄色a一级 | 中文字幕在线看片 | 久久精品国产亚洲 | 欧美性一区二区 | 一级做a爰片久久毛片潮喷 视频一二区 | 天天爽夜夜爽夜夜爽 | 欧美日韩一区二 | 天天操天天干天天 | 在线伊人网 | 伊人春色av | 午夜av片 | 国内精品一区二区三区 | 97人人看 | 久草免费福利视频 | 亚洲va韩国va欧美va精品 | 欧美一级在线观看 | 亚洲三级在线观看 | 欧美成人一区二区三区 | 激情六月| 狠狠综合网| 91精选视频| 精品国产aⅴ麻豆 | 日一日操一操 | 毛片网站免费 | 九九热在线视频 | 日韩欧美国产一区二区三区 | 国产一区二区三区免费视频 | 国产精品视频免费看 | 欧美性影院 | 日韩免费一区二区 | 精品第一页| 国产无遮挡又黄又爽免费网站 | 欧美一级做性受免费大片免费 | 国产福利网站 | 伊人网在线 | 蜜臀久久99精品久久久久久宅男 | 青娱乐av| 婷婷av在线 |