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

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

      1. <small id='5yvG5'></small><noframes id='5yvG5'>

        理解(簡單?)C++ 部分模板專業化

        Understanding (simple?) C++ Partial Template Specialization(理解(簡單?)C++ 部分模板專業化)
        <i id='w4UMf'><tr id='w4UMf'><dt id='w4UMf'><q id='w4UMf'><span id='w4UMf'><b id='w4UMf'><form id='w4UMf'><ins id='w4UMf'></ins><ul id='w4UMf'></ul><sub id='w4UMf'></sub></form><legend id='w4UMf'></legend><bdo id='w4UMf'><pre id='w4UMf'><center id='w4UMf'></center></pre></bdo></b><th id='w4UMf'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='w4UMf'><tfoot id='w4UMf'></tfoot><dl id='w4UMf'><fieldset id='w4UMf'></fieldset></dl></div>

            <tbody id='w4UMf'></tbody>
        • <small id='w4UMf'></small><noframes id='w4UMf'>

            <legend id='w4UMf'><style id='w4UMf'><dir id='w4UMf'><q id='w4UMf'></q></dir></style></legend>
              • <bdo id='w4UMf'></bdo><ul id='w4UMf'></ul>

                  <tfoot id='w4UMf'></tfoot>

                  本文介紹了理解(簡單?)C++ 部分模板專業化的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  注意:這似乎是一個問題的轉貼:C++ - 重載模板化的類方法,部分特化該方法

                  Note: this seems to be a repost of a problem: C++ - Overload templated class method with a partial specilization of that method

                  我已經將我在 C++ 模板專業化中遇到的問題歸結為一個簡單的案例.

                  I have boiled down a problem I am having with C++ template specialization down to a simple case.

                  它由一個簡單的 2 參數模板類 Thing 組成,我想在其中專門化 Thing<A,B>::doSomething() 用于 B=int.

                  It consists of a simple 2-parameter template class Thing, where I would like to specialize Thing<A,B>::doSomething() for B=int.

                  #include <cstdio>
                  
                  // A 3-parameter template class.
                  template <class A, class B>
                  class Thing
                  {
                  public:
                      Thing(A a, B b) : a_(a), b_(b) {}
                      B doSomething();
                  private:
                      A a_;
                      B b_;
                  };
                  
                  // The generic case works as expected.
                  template <class A, class B>
                  B Thing<A,B>::doSomething()
                  {
                      return b_;
                  }
                  
                  // This specialization does not work!
                  template <class A>
                  int Thing<A,int>::doSomething()
                  {
                      return b_+1;
                  }
                  
                  int main() {
                      // Setup our thing.
                      Thing<double,int> thing(1.0,2);
                      // This doesn't compile - but works with the generic case.
                      printf("Expecting 3, and getting %i
                  ", thing.doSomething());
                      // Clean up.
                      return 0;
                  }
                  

                  不幸的是,g++ 退出并出現錯誤:

                  Unfortunately, g++ exits with the error:

                  partial_specialization.cpp:30: error: invalid use of incomplete type ‘class Thing<A, int>’
                  partial_specialization.cpp:8: error: declaration of ‘class Thing<A, int>’
                  

                  clang++ 編譯器有點冗長,但也有同樣的問題:

                  The clang++ compiler is a bit more verbose, but has the same problem:

                  partial_specialization.cpp:30:19: error: nested name specifier 'Thing<A, int>::' for declaration does not
                        refer into a class, class template or class template partial specialization
                  int Thing<A,int>::doSomething()
                      ~~~~~~~~~~~~~~^
                  partial_specialization.cpp:32:12: error: use of undeclared identifier 'b_'
                      return b_+1;
                             ^
                  2 errors generated.
                  

                  我已閱讀并了解不允許對函數進行部分模板特化 - 但我認為在這種情況下我部分特化了 Thing 的類.

                  I have read and understood that partial template specializations on functions aren't allowed - but I thought I was partially specializing over classes of Thing in this case.

                  有什么想法嗎?

                  我做了什么:一種解決方法,根據接受的答案提供的鏈接確定:

                  What I did: A workaround, as determined from the link provided by the accepted answer:

                  template< class T >
                  inline T foo( T const & v ) { return v; }
                  
                  template<>
                  inline int foo( int const & v ) { return v+1; }
                  
                  // The generic case works as expected.
                  template <class A, class B>
                  B Thing<A,B>::doSomething()
                  {
                      return foo(b_);
                  }
                  

                  推薦答案

                  函數模板的部分特化,無論是成員函數模板還是獨立函數模板,都是標準不允許的:

                  Partial specialization of a function template, whether it is member function template or stand-alone function template, is not allowed by the Standard:

                  template<typename T, typename U> void f() {} //okay  - primary template
                  template<typename T> void f<T,int>() {}      //error - partial specialization
                  template<> void f<unsigned char,int>() {}    //okay  - full specialization
                  

                  但是您可以部分特化類模板本身.你可以這樣做:

                  But you can partially specialize the class template itself. You can do something like this:

                  template <class A>
                  class Thing<A,int>  //partial specialization of the class template
                  {
                      //..
                      int doSomething();
                  };
                  
                  template <class A>
                  int Thing<A,int>::doSomething()  { /* do whatever you want to do here */ }
                  

                  注意,當你部分特化一個類模板時,成員函數的模板參數列表(在類外的定義中),必須匹配類模板部分特化的模板參數列表.這意味著,對于類模板的上述部分特化,你不能定義這個:

                  Note that when you partially specialize a class template, then the template parameter-list of member function (in its definition outside the class), must match the template parameter list of the class template partial specialization. That means, for the above partial specialization of the class template, you cannot define this:

                  template <class A>
                  int Thing<A,double>::doSomething(); //error
                  

                  這是不允許的,因為函數定義中的模板參數列表與類模板部分特化的模板參數列表不匹配.標準 (2003) 中的 §14.5.4.3/1 說,

                  Its not allowed, because the template parameter-list in function definition didn't match the template parameter-list of the class template partial specialization. §14.5.4.3/1 from the Standard (2003) says,

                  類模板部分特化成員的模板參數列表應匹配類模板部分特化的模板參數列表.[...]

                  The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization.[...]

                  有關這方面的更多信息,請在此處閱讀我的回答:

                  For more on this, read my answer here:

                  C++- 重載模板化類方法,使用該方法的部分專業化

                  那么解決方案是什么?除了所有重復性工作外,您會部分專業化您的課程嗎?

                  So what is the solution? Would you partially specialize your class along with all the repetitive work?

                  一個簡單的解決方案是工作委托,而不是部分專門化類模板.編寫一個獨立函數模板并將其特化為:

                  A simple solution would be work delegation, instead of partially specializing the class template. Write a stand-alone function template and specialize this as:

                  template <class B>
                  B doTheActualSomething(B & b) { return b;  }
                  
                  template <>
                  int doTheActualSomething<int>(int & b) { return b + 1; }
                  

                  然后從 doSomething() 成員函數調用這個函數模板:

                  And then call this function template from doSomething() member function as:

                  template <class A, class B>
                  B Thing<A,B>::doSomething() { return doTheActualSomething<B>(b_); }
                  

                  <小時>

                  由于在您的特定情況下,doTheActualSomething 需要知道只有一個 成員的值,即 b_,上述解決方案就可以了,因為您可以將值作為參數傳遞給函數,其類型是模板 type 參數 B,并且 int 的特化可能是完整的-專業化.


                  Since in your particular case, doTheActualSomething needs to know the value of only one member, namely b_, the above solution is fine, as you can pass the value to the function as argument whose type is the template type argument B, and specialization for int is possible being it full-specialization.

                  但是想象一下,如果需要訪問多個成員,每個成員的type取決于模板type參數列表,那么定義一個獨立的函數模板就不會了解決問題,因為現在函數模板將有多個 type 參數,并且您不能部分將函數專門化為一個類型(因為它不允許).

                  But imagine if it needs to access multiple members, type of each depends on the template type argument-list, then defining a stand-alone function template wouldn't solve the problem, because now there will be more than one type argument to the function template, and you cannot partially specialize the function for just, say, one type (as its not allowed).

                  所以在這種情況下你可以定義一個類模板,它定義一個靜態非模板成員函數doTheActualSomething.方法如下:

                  So in this case you can define a class template instead, which defines a static non-template member function doTheActualSomething. Here is how:

                  template<typename A, typename B>
                  struct Worker
                  {
                     B doTheActualSomething(Thing<A,B> *thing)
                     {
                        return thing->b_;
                     }
                  };
                  
                  //partial specialization of the class template itself, for B = int
                  template<typename A>
                  struct Worker<A,int>
                  {
                     int doTheActualSomething(Thing<A,int> *thing)
                     {
                        return thing->b_ + 1;
                     }
                  };
                  

                  請注意,您可以使用 thing 指針來訪問類的任何成員.當然,如果它需要訪問私有成員,那么你必須讓 struct Worker 成為 Thing 類模板的朋友,如:

                  Notice that you can use thing pointer to access any member of the class. Of course, if it needs to access private members, then you've to make struct Worker a friend of Thing class template, as:

                  //forward class template declaration
                  template<typename T, typename U> struct Worker
                  
                  template <class A, class B>
                  class Thing
                  {
                      template<typename T, typename U>  friend struct Worker; //make it friend
                     //...
                  };
                  

                  現在將工作委托給朋友:

                  Now delegate the work to the friend as:

                  template <class A, class B>
                  B Thing<A,B>::doSomething()
                  {
                      return Worker<A,B>::doTheActualSomething(this); //delegate work
                  }
                  

                  這里要注意兩點:

                  • 在這個解決方案中,doTheActualSomething 不是成員函數模板.它不是包含模板的類.因此,我們可以隨時部分特化類模板,以獲得部分成員函數模板特化的預期效果.
                  • 由于我們將this指針作為參數傳遞給函數,我們可以訪問Thing類的任何成員,甚至私有成員,作為Worker 也是朋友.
                  • In this solution, doTheActualSomething is not a member function template. Its not enclosing class which is template. Hence we can partially specialize the class template anytime, to get the desired effect of the partial member function template specialization.
                  • Since we pass this pointer as argument to the function, we can access any member of the class Thing<A,B>, even private members, as Worker<T,U> is also a friend.

                  完整的在線演示:http://www.ideone.com/uEQ4S

                  現在仍有改進的機會.現在Worker 類模板的所有實例化都是Thing 類模板的所有實例化的朋友.所以我們可以將這種多對多的友誼限制為:

                  Now there is still a chance of improvement. Now all instantiations of Worker class template are friends of all instantiation of Thing class template. So we can restrict this many-to-many friendship as:

                  template <class A, class B>
                  class Thing
                  {
                      friend struct Worker<A,B>; //make it friend
                     //...
                  };
                  

                  現在只有一個 Worker 類模板的實例是 Thing 類模板的一個實例的朋友.那是一對一的友誼.也就是說,WorkerThing 的朋友.Worker 不是 Thing 的朋友.

                  Now only one instantiation of Worker class template is a friend of one instantiation of Thing class template. That is one-to-one friendship. That is, Worker<A,B> is a friend of Thing<A,B>. Worker<A,B> is NOT a friend of Thing<A,C>.

                  此更改要求我們以稍微不同的順序編寫代碼.查看完整的演示,以及所有類和函數定義的順序:

                  This change requires us to write the code in somewhat different order. See the complete demo, with all the ordering of class and function definitions and all:

                  http://www.ideone.com/6a1Ih

                  這篇關于理解(簡單?)C++ 部分模板專業化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='CZe7V'></small><noframes id='CZe7V'>

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

                  1. <tfoot id='CZe7V'></tfoot>
                      <tbody id='CZe7V'></tbody>
                      • <bdo id='CZe7V'></bdo><ul id='CZe7V'></ul>
                        <legend id='CZe7V'><style id='CZe7V'><dir id='CZe7V'><q id='CZe7V'></q></dir></style></legend>
                          1. 主站蜘蛛池模板: 国精产品一品二品国精在线观看 | 久久综合久色欧美综合狠狠 | 中文在线а√在线8 | 国产精品成人一区二区 | 精品少妇一区二区三区在线播放 | 久久久久久国产 | 欧美精品一区在线发布 | 欧美高清视频一区 | 国产精品日韩欧美一区二区三区 | 成人午夜精品一区二区三区 | 精品一区二区三 | 激情小说综合网 | 欧美在线天堂 | 美人の美乳で授乳プレイ | 日日日干干干 | 亚洲黄色高清视频 | 影视先锋av资源噜噜 | 国产精品久久久久无码av | 久久亚洲欧美日韩精品专区 | 日韩av免费在线观看 | 亚洲一一在线 | 国产成人一区二区三区精 | 日韩免费在线观看视频 | 国产乱码精品一区二区三区中文 | 欧美一级二级三级 | 欧美成人一级视频 | 性国产xxxx乳高跟 | 国产特级毛片aaaaaa喷潮 | 亚洲人在线播放 | 91视频久久| www.99热这里只有精品 | 一级亚洲 | 亚洲欧美中文日韩在线v日本 | 久久久久99| 国外成人在线视频网站 | 亚洲经典一区 | 一区二区三区四区av | 色婷婷综合久久久中字幕精品久久 | 国产成人高清视频 | 91精品国产一区二区三区 | 亚洲人成网亚洲欧洲无码 |