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

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

        為什么我可以從派生類調(diào)用基模板類方法

        Why can I call base template class method from derived class(為什么我可以從派生類調(diào)用基模板類方法)
          <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="qwawimqqmiuu" 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>

                • 本文介紹了為什么我可以從派生類調(diào)用基模板類方法的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

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

                  我決定測(cè)試Effective C++"中的一個(gè)示例,但沒(méi)有得到預(yù)期的結(jié)果.所以,顯然這個(gè)(簡(jiǎn)化的)代碼不應(yīng)該編譯:

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

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

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

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

                  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.

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

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

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

                  解決方案是使表達(dá)式依賴,最簡(jiǎn)單的方法是使用 this-> 進(jìn)行限定:

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

                  由于表達(dá)式現(xiàn)在是相關(guān)的,查找被延遲到第二階段,在該階段替換類型并且 A 是一個(gè)具體類型.另一種選擇是使用定義它的類進(jìn)行限定:

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

                  再次表達(dá)變得依賴,并將在第二階段解決.主要區(qū)別在于,在第二種情況下,調(diào)用是合格的,因此它不使用動(dòng)態(tài)調(diào)度.如果 A<T>::f() 是虛擬的,它仍然會(huì)執(zhí)行 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 編譯器中已知的不符合項(xiàng),未實(shí)現(xiàn)兩階段查找.它將模板內(nèi)的所有查找延遲到第二階段,此時(shí)查找成功.

                  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.

                  這篇關(guān)于為什么我可以從派生類調(diào)用基模板類方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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 不能)

                  <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="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 黄色aaa| 特黄一级片 | 日本三级在线视频 | 成人国产在线 | 午夜在线影院 | 一区二区福利视频 | 日韩一区二区三区在线 | 日韩精品在线看 | 久久久九九| 高清乱码男女免费观看 | 久久久久久久久国产 | 欧美精品www | 亚洲一级大片 | 性做久久久久久久免费看 | 亚洲福利影院 | 九九在线观看高清免费 | 欧美在线免费观看视频 | 一级片黄色 | 亚洲第一毛片 | 青草视频在线观看免费 | 欧美三级 欧美一级 | 自拍偷拍亚洲 | 日韩一区二区三区在线 | 国产aaaaaa| 狠狠操网 | 欧美激情网址 | 中文在线观看免费视频 | 黄色片一级片 | 国产精品人人做人人爽人人添 | 91国内精品 | 欧美日韩国产精品 | 午夜视频免费在线观看 | 欧美一级视频在线观看 | 香蕉视频在线免费看 | www.亚洲成人| 黄色网址免费看 | 久久精品一区二区国产 | 色综合色综合色综合 | 91青青草 | 日韩欧美精品在线 | 性一交一乱一伧老太 |