pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="ukyecom" 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>

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

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

        <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="uw6cksg" 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'>

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

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試重新創建觀察者模式,我可以完美地將參數轉發給觀察者的給定成員函數.

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

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

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

                  這無法編譯,模板參數推導/替換失敗.

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

                  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>> 調用來消除歧義,但我不相信我可以用可變參數來做到這一點模板參數包?

                  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?

                  我怎樣才能讓模板參數推導在這里起作用?

                  推薦答案

                  問題在這里:

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

                  對于這兩行,編譯器不知道您指的是哪個 foo::func.因此,您必須通過強制轉換提供缺少的類型信息(即 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);
                  

                  或者,您可以提供編譯器無法推導出的模板參數,這些參數定義了 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 被認為是相同的類型:作為函數參數.例如,

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

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

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

                  這篇關于消除作為模板參數傳遞的重載成員函數指針的歧義的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 不能)

                  <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="eeecwga" 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>

                          • 主站蜘蛛池模板: 平面钻,法兰钻,三维钻-山东兴田阳光智能装备股份有限公司 | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 | 石牌坊价格石牌坊雕刻制作_石雕牌坊牌楼石栏杆厂家_山东嘉祥石雕有限公司 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 探鸣起名网-品牌起名-英文商标起名-公司命名-企业取名包满意 | 搪瓷搅拌器,搪玻璃搅拌器,搪玻璃冷凝器_厂家-淄博越宏化工设备 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 石油/泥浆/不锈钢防腐/砂泵/抽砂泵/砂砾泵/吸砂泵/压滤机泵 - 专业石油环保专用泵厂家 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 除甲醛公司-甲醛检测-广西雅居环境科技有限公司 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 四川实木门_成都实木门 - 蓬溪聚成门业有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 贵阳用友软件,贵州财务软件,贵阳ERP软件_贵州优智信息技术有限公司 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 大鼠骨髓内皮祖细胞-小鼠神经元-无锡欣润生物科技有限公司 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 软装设计-提供软装装饰和软装配饰及软装陈设的软装设计公司 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 武汉宣传片制作-视频拍摄-企业宣传片公司-武汉红年影视 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 液压油缸-液压缸厂家价格,液压站系统-山东国立液压制造有限公司 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 日本SMC气缸接头-速度控制阀-日本三菱伺服电机-苏州禾力自动化科技有限公司 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 下水道疏通_管道疏通_马桶疏通_附近疏通电话- 立刻通 | 世纪豪门官网 世纪豪门集成吊顶加盟电话 世纪豪门售后电话 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 【连江县榕彩涂料有限公司】官方网站 | 江苏皓越真空设备有限公司| 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司| 香蕉筛|直线|等厚|弧形|振动筛|香蕉筛厂家-洛阳隆中重工 | 防潮防水通风密闭门源头实力厂家 - 北京酷思帝克门窗 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 视觉检测设备_自动化检测设备_CCD视觉检测机_外观缺陷检测-瑞智光电 |