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

    1. <i id='brau2'><tr id='brau2'><dt id='brau2'><q id='brau2'><span id='brau2'><b id='brau2'><form id='brau2'><ins id='brau2'></ins><ul id='brau2'></ul><sub id='brau2'></sub></form><legend id='brau2'></legend><bdo id='brau2'><pre id='brau2'><center id='brau2'></center></pre></bdo></b><th id='brau2'></th></span></q></dt></tr></i><div class="mmasg2k" id='brau2'><tfoot id='brau2'></tfoot><dl id='brau2'><fieldset id='brau2'></fieldset></dl></div>
      <legend id='brau2'><style id='brau2'><dir id='brau2'><q id='brau2'></q></dir></style></legend>
        <bdo id='brau2'></bdo><ul id='brau2'></ul>

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

      <tfoot id='brau2'></tfoot>

        為什么我可以從派生類調用基模板類方法

        Why can I call base template class method from derived class(為什么我可以從派生類調用基模板類方法)
          <bdo id='4oBch'></bdo><ul id='4oBch'></ul>

            <i id='4oBch'><tr id='4oBch'><dt id='4oBch'><q id='4oBch'><span id='4oBch'><b id='4oBch'><form id='4oBch'><ins id='4oBch'></ins><ul id='4oBch'></ul><sub id='4oBch'></sub></form><legend id='4oBch'></legend><bdo id='4oBch'><pre id='4oBch'><center id='4oBch'></center></pre></bdo></b><th id='4oBch'></th></span></q></dt></tr></i><div class="aa2c2wa" id='4oBch'><tfoot id='4oBch'></tfoot><dl id='4oBch'><fieldset id='4oBch'></fieldset></dl></div>
            <tfoot id='4oBch'></tfoot>
            1. <small id='4oBch'></small><noframes id='4oBch'>

            2. <legend id='4oBch'><style id='4oBch'><dir id='4oBch'><q id='4oBch'></q></dir></style></legend>
                  <tbody id='4oBch'></tbody>

                • 本文介紹了為什么我可以從派生類調用基模板類方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我決定測試Effective C++"中的一個示例,但沒有得到預期的結果.所以,顯然這個(簡化的)代碼不應該編譯:

                  I decided to test one of the examples in "Effective C++" and I'm not getting the result I expected. So, apparently this (simplified) code shouldn't compile:

                  template <class T>
                  struct A {
                      void f(){}
                  };
                  
                  template <class T>
                  struct B : public A <T> {
                      void f2() { f(); }   // calling base function - will not compile
                  };
                  

                  以下是解釋(為了簡單起見,類名已更改):

                  Here's the explanation (class names changed for simplicity) :

                  上面的代碼不能編譯,至少在符合標準的編譯器中不能.這樣的編譯器會抱怨 f 不存在.我們可以看到 f 在基類中,但編譯器不會在那里尋找它.

                  The code above won't compile, at least not with conformant compilers. Such compilers will complain that f doesn't exist. We can see that f is in the base class, but compilers won't look for it there.

                  我們需要了解原因.問題是當編譯器遇到類模板 B 的定義時,它們不知道它繼承自哪個類.當然,它是 A,但 T 是一個模板參數,一個直到以后才會知道(當 B 被實例化時).不知道什么T也就是說,沒有辦法知道 A<T> 類是什么樣的.特別是,沒有辦法知道它是否有 f 函數.

                  We need to understand why. The problem is that when compilers encounter the definition for the class template B, they don't know what class it inherits from. Sure, it's A<T>, but T is a template parameter, one that won't be known until later (when B is instantiated). Without knowing what T is, there's no way to know what the class A<T> looks like. In particular, there's no way to know if it has a f function.

                  我的編譯器 (Visual Studio) 不介意...它甚至不顯示任何警告.

                  My compiler (Visual Studio) doesn't mind... It doesn't even show any warnings.

                  以上代碼是否正確?

                  推薦答案

                  template <class T>
                  struct A {
                      void f(){}
                  };
                  
                  template <class T>
                  struct B : public A <T> {
                      void f2() { f(); }   // calling base function - will not compile
                  };
                  

                  在派生模板中,表達式 f() 不依賴于任何模板參數,因此編譯器會在第一階段查找期間嘗試解析它.此時,模板還沒有用類型實例化,編譯器不會查看基本的 A<T>.原因是編譯器不可能知道對于實例化的類型是否存在可能不包含任何成員的 A 特化.

                  In the derived template, the expression f() is not dependent on any template argument, so the compiler attempts to resolve it during the first phase lookup. At this point, the template has not yet been instantiated with the type, and the compiler won't look into the base A<T>. The reason is that the compiler could not possibly know whether for the type of the instantiation there is a specialization of A<T> that might not contain any members.

                  解決方案是使表達式依賴,最簡單的方法是使用 this-> 進行限定:

                  The solution is to make the expression dependent, the simplest way would be to qualify with this->:

                  template <typename T>
                  void B<T>::f2() {  this->f(); }
                  

                  由于表達式現在是相關的,查找被延遲到第二階段,在該階段替換類型并且 A 是一個具體類型.另一種選擇是使用定義它的類進行限定:

                  As the expression is now dependent, lookup is delayed until the second phase, where the type is substituted and A<T> is a concrete type. Another alternative is qualifying with the class where it is defined:

                  template <typename T>
                  void B<T>::f2() { A<T>::f(); }
                  

                  再次表達變得依賴,并將在第二階段解決.主要區別在于,在第二種情況下,調用是合格的,因此它不使用動態調度.如果 A<T>::f() 是虛擬的,它仍然會執行 A<T>::f(),而不是最終的覆蓋.

                  Again the expression becomes dependent and will be resolved during the second phase. The main difference is that in this second case, the call is qualified and thus it does not use dynamic dispatch. If A<T>::f() was virtual it would still execute A<T>::f(), and not the final overrider.

                  代碼正確嗎?不.VS 接受嗎?是的.

                  Is the code correct? No. Does VS accept it? Yes.

                  這是 Visual Studio 編譯器中已知的不符合項,未實現兩階段查找.它將模板內的所有查找延遲到第二階段,此時查找成功.

                  This is a known non-conformance in the Visual Studio compiler, that does not implement two phase lookup. It delays all lookup inside the template to the second phase and at that point lookup succeeds.

                  這篇關于為什么我可以從派生類調用基模板類方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 不能)

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

                    <bdo id='Z3WYa'></bdo><ul id='Z3WYa'></ul>
                  • <tfoot id='Z3WYa'></tfoot>

                    <i id='Z3WYa'><tr id='Z3WYa'><dt id='Z3WYa'><q id='Z3WYa'><span id='Z3WYa'><b id='Z3WYa'><form id='Z3WYa'><ins id='Z3WYa'></ins><ul id='Z3WYa'></ul><sub id='Z3WYa'></sub></form><legend id='Z3WYa'></legend><bdo id='Z3WYa'><pre id='Z3WYa'><center id='Z3WYa'></center></pre></bdo></b><th id='Z3WYa'></th></span></q></dt></tr></i><div class="80ymkww" id='Z3WYa'><tfoot id='Z3WYa'></tfoot><dl id='Z3WYa'><fieldset id='Z3WYa'></fieldset></dl></div>
                        <tbody id='Z3WYa'></tbody>

                          • <legend id='Z3WYa'><style id='Z3WYa'><dir id='Z3WYa'><q id='Z3WYa'></q></dir></style></legend>
                            主站蜘蛛池模板: 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 九爱图纸|机械CAD图纸下载交流中心 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 浇钢砖,流钢砖_厂家价低-淄博恒森耐火材料有限公司 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 托利多电子平台秤-高精度接线盒-托利多高精度电子秤|百科 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 团建-拓展-拓展培训-拓展训练-户外拓展训练基地[无锡劲途] | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 济南宣传册设计-画册设计_济南莫都品牌设计公司 | 苏州西装定制-西服定制厂家-职业装定制厂家-尺品服饰西装定做公司 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | 周口市风机厂,周鼓风机,河南省周口市风机厂 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 压缩空气检测_气体_水质找上海京工-服务专业、价格合理 | 万师讲师网-优质讲师培训师供应商,讲师认证,找讲师来万师 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | 游泳池设备安装工程_恒温泳池设备_儿童游泳池设备厂家_游泳池水处理设备-东莞市君达泳池设备有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 广东护栏厂家-广州护栏网厂家-广东省安麦斯交通设施有限公司 | 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 旋片真空泵_真空泵_水环真空泵_真空机组-深圳恒才机电设备有限公司 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | Type-c防水母座|贴片母座|耳机接口|Type-c插座-深圳市步步精科技有限公司 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊| 3D全息投影_地面互动投影_360度立体投影_水幕灯光秀 | 广东银虎 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 螺旋绞龙叶片,螺旋输送机厂家,山东螺旋输送机-淄博长江机械制造有限公司 | 北京发电车出租-发电机租赁公司-柴油发电机厂家 - 北京明旺盛安机电设备有限公司 | 紫外可见光分光度计-紫外分光度计-分光光度仪-屹谱仪器制造(上海)有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 |