2

假设我有这个模板类:

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

我想专门化它,所以我同样定义:

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

现在,只要我们在谈论小班,就可以了。如果我的课程很长,print()单独专攻会聪明得多。我知道如何使用非成员函数来做到这一点。有没有办法用成员函数做到这一点?

4

4 回答 4

4

一个直接的解决方案是,定义包含您想要专门化的东西的类模板,然后专门化这个类模板(毕竟这将是一个小类):

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

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

然后从中得出:

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;
};

您不再需要专门化此类模板;让它尽可能大(并且合理)。


另一种选择是基于策略的设计,其中您将策略类作为模板参数传递给您的类模板(称为主机类)。

例如,

//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);
    }
};

测试代码:

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();
}

输出:

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

在线演示:http: //ideone.com/r4Zk4

于 2011-09-24T16:14:19.360 回答
4

在您的示例中,您使用的是完全专业化。在这种情况下,您可以这样做:

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

但它不适用于部分专业化。

于 2011-09-24T16:29:18.323 回答
2

您可以专门为双精度打印成员功能:

template< typename T >
class MyClass{
public:
       MyClass(T t&):_t(t){}
    ~MyClass(){}
    void print(){}
private:
    T _t;
};
template< typename T >
void MyClass< T >::print(){/* your specific implementation*/}

template<>
void MyClass< double >::print(){/* your specific implementation*/}

于 2011-09-24T17:08:40.340 回答
1

在类.h

// declaration of template class
template<typename T>
class MyClass
{
public:
    MyClass(T t&):_t(t){}
    ~MyClass(){}
    void print();    // general "declaration". 
                     // don't use inline definition for these case
private:
    T _t;
};

// specialization "declaration" of wanted member function
template<>
void MyClass<double>::print();

#include "class.inl" // implementation of template class

在类.inl

// general "definition" of wanted member function
template<typename T>
void MyClass<T>::print()
{
    cout << _t << endl;
}

在类.cpp

#include "class.h"

// specialization "definition" of wanted member function
// specialization definition of anyone must be here.. not inl file..
void MyClass<double>::print()
{
    cout << "double specialization " << _t << endl;
}
于 2013-01-03T01:54:06.880 回答