How to specialize only some members of a template class?

后端 未结 3 1095
忘了有多久
忘了有多久 2020-12-29 12:44

Code:

template
struct A {
  void f1() {};
  void f2() {};

};

template<>
struct A {
  void f2() {};
};


int main() {
  A<         


        
3条回答
  •  时光说笑
    2020-12-29 13:06

    Consider moving common parts to a base class:

    template 
    struct ABase
    {
        void f1();
    };
    
    
    template 
    struct A : ABase
    {
        void f2();
    }  
    
    
    template <>
    struct A : ABase
    {
        void f2();
    };
    

    You can even override f1 in the derived class. If you want to do something more fancy (including being able to call f2 from f1 code in the base class), look at the CRTP.

提交回复
热议问题