久久久久久久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ù)指針的歧義的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問題描述

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

                  我正在嘗試重新創(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ù)的地址,它無(wú)法根據(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;
                  }
                  

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

                  請(qǐng)注意,我有 Args...UArgs... 因?yàn)槲倚枰軌騻鬟f不一定與類型相同的參數(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)用來(lái)消除歧義,但我不相信我可以用可變參數(shù)來(lái)做到這一點(diǎn)模板參數(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);
                  

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

                  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);
                  

                  或者,您可以提供編譯器無(wú)法推導(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);
                  

                  請(qǐng)注意,您必須使用 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);
                  

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

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

                  這篇關(guān)于消除作為模板參數(shù)傳遞的重載成員函數(shù)指針的歧義的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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)庫(kù))
                  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>

                          • 主站蜘蛛池模板: 欧美精品久久久 | 日韩精品一区二区三区中文字幕 | 国产91久久精品一区二区 | 国产精品久久久久久久久久久久 | 男人天堂手机在线视频 | 久久国产精品一区 | 国产精品久久久久久久免费观看 | 精精国产xxxx视频在线播放7 | 国产精品久久久免费 | 国产精品一区二区三区久久 | 一区二区三区欧美 | 国产一区二区久久 | 精品欧美一区二区中文字幕视频 | 国产在线一级片 | av一区二区三区四区 | 欧美xxxx性 | 国产永久免费 | 色婷婷狠狠 | 欧美亚洲国产日韩 | 欧美lesbianxxxxhd视频社区 | 成人激情视频免费观看 | 日日夜夜精品视频 | 亚洲国产精品视频 | 999国产视频 | 国产.com| 亚洲国产精品区 | 野狼在线社区2017入口 | 国产激情小视频 | 一区二区免费在线视频 | 久久精品播放 | 久久机热 | 天天爽天天干 | 青青草原精品99久久精品66 | 国产成人在线播放 | 亚洲国产成人精品一区二区 | 中文字幕日韩一区 | 亚洲一区二区在线视频 | 日韩中文字幕在线观看视频 | 国产免费福利在线 | 成人免费视频网站在线观看 | 国产成人99久久亚洲综合精品 |