zoukankan      html  css  js  c++  java
  • 测试一下 Intel C++8.0 对模板的支持程度zz

    有关模板的语法很多很杂,无法一一列举,在此仅测试几个简单常用的语法。
    以下有关模板的语法分别使用 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 对模板的支持程度
  • 相关阅读:
    windows10装机小记
    Linus Benedict Torvalds hate FUD
    营销文章good
    商城趣聊4
    商城趣聊3
    商城趣聊2
    商城趣聊1
    temp
    学习代码检视方法 (摘自某图片)
    xilinx sdk闪退问题
  • 原文地址:https://www.cnblogs.com/dayouluo/p/155092.html
Copyright © 2011-2022 走看看