測試一下 Intel C++8.0 對模板的支援程度zz

來源:互聯網
上載者:User
文章目錄
  • 評論
有關模板的文法很多很雜,無法一一列舉,在此僅測試幾個簡單常用的文法。

以下有關模板的文法分別使用 Dev-CPP4991、VC++6.0 和 Intel C++8.0 進行測試,DEVCPP和ICC都能完全通過測試,VC++6有部分通不過測試。

1. 模板類靜態成員

template <typename T> struct testClass {

    static int _data;

};

template<> int testClass<char>::_data = 1;

template<> int testClass<long>::_data = 2;

int main( void ) {

    cout << boolalpha << (1==testClass<char>::_data) << endl;

    cout << boolalpha << (2==testClass<long>::_data) << endl;

}

2. 模板類偏特化

template <class I, class O> struct testClass {

    testClass() { cout << "I, O" << endl; }

};

template <class T> struct testClass<T*, T*> {

    testClass() { cout << "T*, T*" << endl; }

};

template <class T> struct testClass<const T*, T*> {

    testClass() { cout << "const T*, T*" << endl; }

};

int main( void ) {

    testClass<int, char> obj1;

    testClass<int*, int*> obj2;

    testClass<const int*, int*> obj3;

}

[注]: VC++6 編譯不通過

3. function template partial order

template <class T> struct testClass {

    void swap( testClass<T>& ) { cout << "swap()" << endl; }

};

template <class T> inline void swap( testClass<T>& x, testClass<T>& y ) {

    x.swap( y );

}

int main( void ) {

    testClass<int> obj1;

    testClass<int> obj2;

    swap( obj1, obj2 );

}

[注]: VC++6 編譯不通過

4. 類成員函數模板

struct testClass {

    template <class T> void mfun( const T& t ) {

        cout << t << endl;

    }

    template <class T> operator T() {

        return T();

    }

};

int main( void ) {

    testClass obj;

    obj.mfun( 1 );

    int i = obj;

    cout << i << endl;

}

[注]: 對於第二個成員函數模板,VC++6 運行異常

5. 預設模板參數推導

template <class T> struct test {

    T a;

};

template <class I, class O=test<I> > struct testClass {

    I b;

    O c;

};

6. 非類型模板參數

template <class T, int n> struct testClass {

    T _t;

    testClass() : _t(n) {

    }

};

int main( void ) {

    testClass<int,1> obj1;

    testClass<int,2> obj2;

}

7. 空模板參數

template <class T> struct testClass;

template <class T> bool operator==( const testClass<T>&, const testClass<T>& ) {

    return false;

};

template <class T> struct testClass {

    friend bool operator== <>( const testClass&, const testClass& );

};

[注]: VC++6 編譯不通過

8. 模板特化

template <class T> struct testClass {

};

template <> struct testClass<int> {

};

9.

template <template <class T> class X>

    struct Widget{

};

[注]: VC++6 編譯不通過

10. [hpho]提供的一個案例

struct Widget1 {

template<typename T>

    T foo(){}

};

template<template<class T>class X>

    struct Widget2{

};

[注]: VC++6 編譯不通過
posted on 2005-01-27 00:24 周星星 閱讀(1853) 評論(28)  編輯 收藏
評論# re: 測試一下 Intel C++8.0 對模板的支援程度 2005-01-27 01:29 七貓 http://www.meta-comm.com/engineering/boost-regression/developer/algorithm-string.html

 re: 測試一下 Intel C++8.0 對模板的支援程度

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.