woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

测试一下 Intel C++8.0 对模板的支持程度

有关模板的语法很多很杂,无法一一列举,在此仅测试几个简单常用的语法。
以下有关模板的语法分别使用 Dev-CPP4991VC++6.0 Intel C++8.0 进行测试,DEVCPPICC都能完全通过测试,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 2008-09-15 13:07 肥仔 阅读(309) 评论(0)  编辑 收藏 引用 所属分类: C++ 模板


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理