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 <>
void MyClass<double>::print()
{
  cout << _t << endl;
}

ただし、部分的な特殊化には機能しません。

于 2011-09-24T16:29:18.323 に答える
4

簡単な解決策の 1 つは、特殊化したいものを含む基本クラス テンプレートを定義し、代わりにこのクラス テンプレートを特殊化することです (結局のところ、これは小さなクラスになります)。

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

このクラス テンプレートを特殊化する必要はなくなりました。必要なだけ大きくします(そして合理的です)。


もう 1 つの方法は、ポリシークラスをテンプレート引数としてクラス テンプレート (ホストクラスと呼ばれる) に渡すポリシーベースの設計です。

例えば、

//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 に答える
2

print メンバー関数を double 用に特殊化できます。

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

class.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

class.inlで

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

class.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 に答える