Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial template specialization - member specialization

Say I have this template class:

template<typename T> class MyClass{
public:
    MyClass(const T& t):_t(t){}
    ~MyClass(){}
    void print(){ cout << _t << endl; }
private:
    T _t;
};

And I want to specialize it, so similarly I define:

template<> class MyClass<double>{
    public:
        MyClass(const double& t):_t(t){}
        ~MyClass(){}
        void print(){ cout << _t << endl; }
    private:
        double _t;
};

Now, this is ok as long as we're talking about small classes. If I have a very long class, it would be a lot smarter to specialize print() alone. I know how to do it with non-member function. Is there any way to do it with member functions?

like image 293
yotamoo Avatar asked Apr 26 '26 10:04

yotamoo


2 Answers

In your example, you are using full specialization. In that case, you can do it like this:

template <>
void MyClass<double>::print()
{
  cout << _t << endl;
}

but it doesn't work for partial specialization.

like image 192
Vaughn Cato Avatar answered Apr 29 '26 00:04

Vaughn Cato


One straightforward solution is, define base class template containing things which you want to specialize, and then specialize this class template instead (it would be a small class, after all):

template<typename T> 
struct printable
{
 protected:
   void print(const T & _t)  { }
};

template<> 
struct printable<double>
{
 protected:
   void print(const double & _t)  { }
};

And then derived from it:

template<typename T> 
class MyClass : public printable<T>
{
   typedef printable<T> base;
public:
    MyClass(T t&):_t(t){}
    ~MyClass(){}
    void print(){ base::print(_t); } //forward
private:
    T _t;
};

You don't need to specialize this class template anymore; make it as huge as you want (and reasonable).


Another alternative is policy-based design in which you pass policy-class(es) as template argument(s) to your class template (called host class).

For example,

//lets define few policy classes
struct cout_print_policy
{
    template<typename T>
    static void print(T const & data)
    {
       std::cout << "printing using cout = " << data << std::endl;
    }
};

struct printf_print_policy
{
    static void print(int data)
    {
       std::printf("printing int using printf = %d\n", data);
    }
    static void print(double data)
    {
       std::printf("printing double using printf = %f\n", data);
    }
};

//now define the class template (called host class) that 
//accepts policy as template argument
template<typename T, typename TPrintPolicy>
class host
{
    typedef TPrintPolicy print_policy;
    T data;
 public:
    host(T const & d) : data(d) {}
    void print() 
    {
        print_policy::print(data);
    }
};

Test code:

int main()
{
  host<int, cout_print_policy>      ic(100);
  host<double, cout_print_policy>   dc(100.0);

  host<int, printf_print_policy>    ip(100);
  host<double, printf_print_policy> dp(100.0);

  ic.print();
  dc.print();
  ip.print();
  dp.print();
}

Output:

printing using cout = 100
printing using cout = 100
printing int using printf = 100
printing double using printf = 100.000000

Online demo : http://ideone.com/r4Zk4

like image 36
Nawaz Avatar answered Apr 29 '26 00:04

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!