3

I have a template class, and at least 95% codes of it is same for all types of the template-parameter, unless a member-variable and a function should be added for one specialization.

The sample I want to get is following:

template <typename T>
class AClass {
private:
   T  payLoad;

public:
   AClass( const T& crp_payload ) : payLoad( crp_payload ) {};

   void showMe() {
      cout << "Common showMe:" << payLoad << endl;
   };

   /*
    * A lot of functions same for all specializations go here.
    * I absolutely don't want to implement respectively them for
    * every specializations!
    */

// specializing for int ----------------------------
   // dedicated function
   template <int>
   void showPayload() {
      cout << "AClass<int>::showPayload:" << payLoad << endl;
   };
   // dedicated variable, but following code can not be compiled!
   template <int>
   int   otherPayload;
};

int main() {
   AClass<int> iac( 123 );
   iac.showMe();
   iac.showPayload();//can not pass the compiling!

   AClass<float> fac(456);
   fac.showMe();
   return 0;
};

My questions:

  1. How to add merely "otherPayload" variable without re-coding entire AClass<int>?
  2. How to call showPayload() sinc I get a error msg when I do it in main() as above.
  3. Is there no way only by specializing to "revise/supplement" some members to a class without totally re-implement it?
Leon
  • 1,489
  • 1
  • 12
  • 31

2 Answers2

4

One possible way would be the good old inheritance:

template<class T> struct Extra {};

template<> struct Extra<int> {
    int extraPayload;
    void showPayload();
};

template<class T> class Class: public Extra<T> {
    void showMe();
};

template<> void Class<int>::showMe() { showPayload(); }

All the specialization-specific parts are extracted in a separate class, and common methods are specialized as needed.

bipll
  • 11,747
  • 1
  • 18
  • 32
4

I think you can simply do normal specialization of the template-class:

#include <iostream>
#include <iomanip>

template <typename T>
class BaseClass 
{
protected:
    T  payLoad;

public:
    BaseClass(const T& crp_payload)
        : payLoad( crp_payload )
    { }

    void showMe() {
        std::cout << "Common showMe:" << payLoad << std::endl;
    }


   /*
    * A lot of functions same for all specializations go here.
    * I absolutely don't want to implement respectively them for
    * every specializations!
    */
};

template <typename T>
class AClass
    : public BaseClass<T> 
{  
public:
    AClass( const T& crp_payload )
        : BaseClass<T>(crp_payload)
    { }

};

// specializing for int ----------------------------
template<>
class AClass<int>
    : public BaseClass<int>
{
public:
    AClass( int crp_payload )
        : BaseClass(crp_payload)
    { }

   // dedicated function
   void showPayload() {
      std::cout << "AClass<int>::showPayload:" << payLoad << std::endl;
   }
private:
   int   otherPayload;
};

int main() {
   AClass<int> iac( 123 );
   iac.showMe();
   iac.showPayload();//can not pass the compiling!

   AClass<float> fac(456);
   fac.showMe();
   return 0;
}
wilx
  • 17,697
  • 6
  • 59
  • 114
  • Thanks for your answer! I think that your solution is the best so far. Indeed, I want to make sure that there is no way only by specializing to "revise/supplement" some members to a class without totally re-implement it. Is it? – Leon Apr 09 '19 at 09:15
  • @Leon You can factor out the common code into a base class as I did in the answer. – wilx Apr 09 '19 at 18:24