C++ template specialization of constructor

后端 未结 7 1008
醉话见心
醉话见心 2021-02-05 17:15

I have a templated class A and two typedefs A and A. How do I override the constructor for A ? The following does not wor

7条回答
  •  耶瑟儿~
    2021-02-05 17:50

    The best solution I've been able to come up with for this situation is to use a "constructor helper function":

    template  class A;
    typedef  A one_type;
    typedef  A second_type;
    
    template 
    class A {
    private:
      void cons_helper(int m) {test= (m>M);}
    public:
      A(int m) { cons_helper(m); }
    
      bool test;
    };
    
    template <>
    void one_type::cons_helper(int) { cerr << "One type" << endl;}
    

提交回复
热议问题