c++中的特化問題

fantasy_linux發表於2016-04-26
   C++類别範本的三種特化:

      (1)特化為絕對型別:即直接為某個特定型別做特化。

    下面給出程式碼例項:

template<typename T>
class Test
{
public:
    Test(T t = 0):data(t)
    {
        cout << "Test object!" << endl;
    }
private:
    T data;
};


template<class T>
class Compare
{
public:
    static bool IsEqual(const T& lh,const T& rh)
    {
        cout << "Compare<T>!" << endl;
        return lh == rh;
    }
};

template<>
class Compare<float>
{
public:
    static bool IsEqual(const float& lh,const float& rh)
    {
        cout << "Compare<float>!" << endl;
        return abs(lh  - rh) < 10e-3;
    }
};

template<>
class Compare<double>
{
public:
    static bool IsEqual(const double& lh,const double& rh)
    {
        cout << "Compare<double>!" << endl;
        return abs(lh - rh) < 10e-6;
    }
};


int main(int argc,char**argv)
{
    Compare<int> a;
    a.IsEqual(5,3);

    Compare<float> b;
    b.IsEqual(5.1f,3.3f);

    Compare<double> c;
    c.IsEqual(5.1,3.3);

    return 0;
}

程式碼的執行結果如下:

     (2)特化為引用,指標型別。

 

template<class T>
class Compare
{
public:
    static bool IsEqual(const T& lh,const T& rh)
    {
        cout << "Compare<T>!" << endl;
        return lh == rh;
    }
};

template<>
class Compare<int>
{
public:
    static bool IsEqual(const int& lh,const int& rh)
    {
        cout << "Compare<int>!" << endl;
        return lh == rh;
    }
};

template<class T>
class Compare<T&>
{
public:
    static bool IsEqual(const T& lh,const T& rh)
    {
        cout << "Compare<T&>!" << endl;
        return Compare<T>::IsEqual(lh,rh);
    }
};

template<class T>
class Compare<T*>
{
public:
    static bool IsEqual(const T* lh,const T* rh)
    {
        cout << "Compare<T*>!" << endl;
        return Compare<T>::IsEqual(*lh,*rh);
    }
};

int main(int argc,char**argv)
{
    int * p1 = new int(10);
    int * p2 = new int(20);
    Compare<int*> c;
    c.IsEqual(p1,p2);    

    int a = 10;
    int b = 3;
    int &a1 = a;
    int &b1 = b;
    Compare<int&> d;
    d.IsEqual(a1,b1);

    return 0;
}

        除了特化為T*,我們也可以轉化為const T*,T&,const T&。

    (3)轉化為另一個類的模板

template<class T>
class Test
{
public:
    Test(T t = T()):data(t)
    {
        cout << "Test object!" << endl;
    }
public:
    bool operator==(const T& t)
    {
        return this->data == t.data;
    }
    T get_data()const
    {
        return data;
    }
private:
    T data;
};


template<class T>
class Compare
{
public:
    static bool IsEqual(const T& lh,const T& rh)
    {
        cout << "Compare<T>!" << endl;
        return lh == rh;
    }
};

template<class T>
class Compare<vector<T> >
{
public:
    static bool IsEqual(const vector<T>& lh,const vector<T>& rh)
    {
        cout << "Compare<vector<T> >!"<< endl;
        if(lh.size() != rh.size()){
            return false;
        }else{
            for(int i = 0; i < lh.size(); ++i){
                if(lh[i] != rh[i]) return false;
            }
        }
        return true;
    }
};

template<class T>
class Compare<Test<T> >
{
public:
    static bool IsEqual(const Test<T>& lh,const Test<T>& rh)
    {
        cout << "Compare<Test<T> >!" << endl;
        return lh.get_data() == rh.get_data();
    }
};

int main(int argc,char**argv)
{
    Test<int> a(10);
    Test<int> b(15);

    Compare<Test<int> > c;
    c.IsEqual(a,b);
    return 0;
}
#endif




 以上就是C++類别範本特化的三種形式。。。。


我們下面來看類别範本成員函式的特化:

//類别範本成員函式的特化
class Test
{
public:
    Test()
    {
        cout << "Test()!" << endl;
    }
};


class Sample2
{
public:
    template<class T>
    void print()
    {
        cout << "Sample2 print template!" << endl;
    }
};

template<>
void Sample2::print<double>()
{
    cout << "Sample2 print char!" << endl;
}

template<>
void Sample2::print<int>()
{
    cout << "Sample2 print int!" << endl;
}

template<>
void Sample2::print<char>()
{
    cout << "Sample2 print char!" << endl;
}

template<>
void Sample2::print<Test>()
{
    cout << "Sample2 print Test!" << endl;
}

int main(int argc,char**argv)
{
    Sample2 sam2;
    sam2.print<double>();
    sam2.print<int>();
    sam2.print<char>();
    sam2.print<Test>();
    return 0;
}

類的成員模板函式的特化:

//類的成員模板函式的特化
class Test
{
public:
    Test()
    {
        cout << "Test()!" << endl;
    }
};


template<class T>
class Sample
{
public:
    void print()
    {
        cout << "print template!" << endl;
    }
};

template<>
void Sample<int>::print()
{
    cout << "print int!" << endl;
}

template<>
void Sample<Test>::print()
{
    cout << "print Test!" << endl;
}

int main(int argc,char**argv)
{
    Sample<int> a;
    a.print();

    Sample<double> b;
    b.print();

    Sample<Test> c;
    c.print();
    return 0;
}


相關文章