<tfoot id='3J6jo'></tfoot>
      <bdo id='3J6jo'></bdo><ul id='3J6jo'></ul>

    <small id='3J6jo'></small><noframes id='3J6jo'>

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

      2. 基于继承类的模板特化

        时间:2023-05-25
      3. <tfoot id='49Bp1'></tfoot>

            • <bdo id='49Bp1'></bdo><ul id='49Bp1'></ul>
                <tbody id='49Bp1'></tbody>

                  <small id='49Bp1'></small><noframes id='49Bp1'>

                  <legend id='49Bp1'><style id='49Bp1'><dir id='49Bp1'><q id='49Bp1'></q></dir></style></legend>
                  <i id='49Bp1'><tr id='49Bp1'><dt id='49Bp1'><q id='49Bp1'><span id='49Bp1'><b id='49Bp1'><form id='49Bp1'><ins id='49Bp1'></ins><ul id='49Bp1'></ul><sub id='49Bp1'></sub></form><legend id='49Bp1'></legend><bdo id='49Bp1'><pre id='49Bp1'><center id='49Bp1'></center></pre></bdo></b><th id='49Bp1'></th></span></q></dt></tr></i><div id='49Bp1'><tfoot id='49Bp1'></tfoot><dl id='49Bp1'><fieldset id='49Bp1'></fieldset></dl></div>

                  本文介绍了基于继承类的模板特化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想让这个专门的不改变主.是否可以根据其基类专门化某些东西?我希望如此.

                  I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.

                  -编辑-

                  我将有几个继承自 SomeTag 的类.我不想为他们每个人编写相同的专业.

                  I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.

                  class SomeTag {};
                  class InheritSomeTag : public SomeTag {};
                  
                  template <class T, class Tag=T>
                  struct MyClass
                  {
                  };
                  
                  template <class T>
                  struct MyClass<T, SomeTag>
                  {
                      typedef int isSpecialized;
                  };
                  
                  int main()
                  {
                      MyClass<SomeTag>::isSpecialized test1; //ok
                      MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()
                      return 0;
                  }
                  

                  推荐答案

                  这篇文章描述了一个巧妙的技巧:http://www.gotw.ca/publications/mxc++-item-4.htm

                  This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm

                  这是基本思想.您首先需要一个 IsDerivedFrom 类(它提供运行时和编译时检查):

                  Here's the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking):

                  template<typename D, typename B>
                  class IsDerivedFrom
                  {
                    class No { };
                    class Yes { No no[3]; }; 
                  
                    static Yes Test( B* ); // not defined
                    static No Test( ... ); // not defined 
                  
                    static void Constraints(D* p) { B* pb = p; pb = p; } 
                  
                  public:
                    enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) }; 
                  
                    IsDerivedFrom() { void(*p)(D*) = Constraints; }
                  };
                  

                  那么你的 MyClass 需要一个潜在的特殊实现:

                  Then your MyClass needs an implementation that's potentially specialized:

                  template<typename T, int>
                  class MyClassImpl
                  {
                    // general case: T is not derived from SomeTag
                  }; 
                  
                  template<typename T>
                  class MyClassImpl<T, 1>
                  {
                    // T is derived from SomeTag
                    public:
                       typedef int isSpecialized;
                  }; 
                  

                  和 MyClass 实际上看起来像:

                  and MyClass actually looks like:

                  template<typename T>
                  class MyClass: public MyClassImpl<T, IsDerivedFrom<T, SomeTag>::Is>
                  {
                  };
                  

                  那么你的主菜就可以了:

                  Then your main will be fine the way it is:

                  int main()
                  {
                      MyClass<SomeTag>::isSpecialized test1; //ok
                      MyClass<InheritSomeTag>::isSpecialized test2; //ok also
                      return 0;
                  }
                  

                  这篇关于基于继承类的模板特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:模板是什么意思?在 C++ 中使用空尖括号? 下一篇:使用模板技巧访问私人成员

                  相关文章

                  最新文章

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

                  1. <i id='JahV3'><tr id='JahV3'><dt id='JahV3'><q id='JahV3'><span id='JahV3'><b id='JahV3'><form id='JahV3'><ins id='JahV3'></ins><ul id='JahV3'></ul><sub id='JahV3'></sub></form><legend id='JahV3'></legend><bdo id='JahV3'><pre id='JahV3'><center id='JahV3'></center></pre></bdo></b><th id='JahV3'></th></span></q></dt></tr></i><div id='JahV3'><tfoot id='JahV3'></tfoot><dl id='JahV3'><fieldset id='JahV3'></fieldset></dl></div>

                  2. <legend id='JahV3'><style id='JahV3'><dir id='JahV3'><q id='JahV3'></q></dir></style></legend>

                      • <bdo id='JahV3'></bdo><ul id='JahV3'></ul>

                      <tfoot id='JahV3'></tfoot>