zoukankan      html  css  js  c++  java
  • *** 模板特例化

    //设基本模板如下
    template <class A, class B, class C> class X{};

    全部模板特例化

    模板中所有参数全被指定为确定的类型

    template<> 
    
    class X<int, float, double>{} 

    部分模板特例化分两种情况

    1.对部分模板参数进行特例化

    template <class B, class C>
    class X<int, B, C>{};

    2.使用具有某一特征的类型,对模板参数进行特例化

    template<class A, class B, class C>
    class X<A*,B*,C>{};
    //当模板实参中前两个是指针时,编译器使用本特例化模板

    混合这两种情况的例子如下

    template<class A, class B>
    class X<A*,B*,int>{};

    总结:

    template后<>的列表中要列出没有明确指明类型的 typename

    下面是一段示例代码:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    template<class T>
    class A
    {
    public:
        T Max(T a, T b)
        {
            return (a>b)?(a):(b);
        }    
    };
    
    //class A针对类型char*的特例化
    template<>
    class A<char*>
    {
    public:
        char * Max(char *a, char *b)
        {
            return (strcmp(a,b))?(a):(b);
        }    
    };
    
    int main()
    {
        A<int> obj;
        int x, y;
        cout << "Enter 1st integer:" << endl;
        cin >> x;
        cout << "Enter 2nd integer:" << endl;
        cin >> y;
        cout << "The bigger one is " << obj.Max(x,y) << endl;
    
        return 0;
    }

    下面是另一段sample代码:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    template<typename A, typename B>
    class Base
    {
    public:
        Base(B x):value(x){}
        
        bool compare(A x, A y)
        {
            return (x>y)?(true):(false);
        }
        
        void print(void)
        {
            cout << "Basic template:" << value << endl;
        }
        
        B value;
    };
    
    template<typename A>
    class Base<A*,int>
    {
    public:
        Base(int x):value(x){}
        bool compare(A* x, A* y)
        {
            return *x > *y ? true : false;
        }
        
        void print(void)
        {
            cout << "specialized template:" << value << endl;
        }
        
        int value;
    };
    
    int main()
    {
        // basic template
        Base<int,float> a(2.1);
        a.print();    
        if (a.compare(39, 29))
            cout << "$1 > $2" << endl;
        else
            cout << "$1 <= $2" << endl;
    
        // specialized template
        Base<float*,int> b(7);
        float v1 = 1.2, v2=2.8;
        b.print();
        if (b.compare(&v1, &v2))
            cout << "$1 > $2" << endl;
        else
            cout << "$1 <= $2" << endl;
    
        return 0;
    }
  • 相关阅读:
    java1.8时间比较应用
    Window配置网络设定IPv4的固定IP自动被修改为169.254.*.*的问题
    osgi内嵌jetty容器添加过滤器
    jackson依赖的jar包
    Tomcat下ajax请求路径总结
    JavaScript中一个字符串变量突然变成了false的问题解析
    Ajax请求发送的FormData是"[object object]"
    jQuery的$.extend方法使用
    JS 异常:Uncaught RangeError: Maximum call stack size exceeded解析
    Java的重写equals但不重写hashCode方法的影响
  • 原文地址:https://www.cnblogs.com/superrunner/p/10164856.html
Copyright © 2011-2022 走看看