使用模版类作为模版参数

#include <iostream>
 
//-------------------
 
namespace trait {
    //通用模版
    template <typename T>
    struct type_init{};
 
     //实例化int模版
    template <>
    struct type_init<int> {
 
        enum {
            value = 0
        };
    };
     
     //实例化unsigned int模版
    template <>
    struct type_init<unsigned int> {
 
        enum {
            value = 0
        };
    };
 
     //实例化char模版
    template <>
    struct type_init<char> {
 
        enum {
            value = 0
        };
    };
 
     //实例化unsigned char模版
    template <>
    struct type_init<unsigned char> {
 
        enum {
            value = 0
        };
    };
     
     //通用模版
    template <class T>
    struct type_addition {
        typedef T value_type;
        typedef T* pointer;
        typedef T& reference;
        typedef const T* const_pointer;
        typedef const T& const_reference;
        //std::cout << "normal template ! T=" << typeid(T).name()<< std::endl;
        //printf("normal template ! T=%s\n", typeid(T).name());
    };
 
     //实例化void模版
    template <>
    struct type_addition<void> {
        typedef void value_type;
        typedef void* pointer;
        typedef const void* const_pointer;
        //std::cout << "void template ! T=void" << std::endl;
        //printf("void template !\n");
    };

    //实例化int模版
    template <>
    struct type_addition<int> {
        typedef int value_type;
        typedef int* pointer;
        typedef int& reference;
        typedef const int* const_pointer;
        typedef const int& const_reference;
       
    };
 
     //实例化T*模版
    template <class T>
    struct type_addition<T*> {
        typedef T value_type;
        typedef T* pointer;
        typedef T& reference;
        typedef const T* const_pointer;
        typedef const T& const_reference;
        //cout << "T* template ! T=" << typeid(T).name()<< endl;
        //printf("T* template ! T= %s\n", typeid(T).name());
    };
     
     //实例化T&模版
    template <class T>
    struct type_addition<T&> {
        typedef T value_type;
        typedef T* pointer;
        typedef T& reference;
        typedef const T* const_pointer;
        typedef const T& const_reference;
        //cout << "T& template ! T=" << typeid(T).name()<< endl;
        //printf("T& template ! T= %s\n", typeid(T).name());
    };
 
     //实例化const T*模版
    template <class T>
    struct type_addition<const T*> {
        typedef T value_type;
        typedef T* pointer;
        typedef T& reference;
        typedef const T* const_pointer;
        typedef const T& const_reference;
        //cout << "const T* template ! T=" << typeid(T).name()<< endl;
        //printf("const T* template ! T= %s\n", typeid(T).name());
    };
 
}
 
//-------------------
 
/**定义模版类*/
template <typename T, typename E>
class Node {
public:
 
    void sayHello(void) {        
        std::cout << "hello ! T=" << typeid(T).name()<<" E=" << typeid(E).name()<< std::endl;
    }
};
 
/**特化模版类 int*/
template <>
class Node<int, int> {
public:
 
    void sayHello(void) {
        std::cout << "hello <int, int> !" << std::endl;;
    }
};

/**特化模版类 char*/
template <>
class Node<int, char> {
public:
 
    void sayHello(void) {
        std::cout << "hello <int, char> !" << std::endl;;
    }
};

/**特化模版类 void*/
template <>
class Node<int, void> {
public:
 
    void sayHello(void) {
        std::cout << "hello <int, void> !" << std::endl;;
    }
};
 
/**创建模版类,其中模版的参数N也是一个模版类,且默认为Node<typanem T,typename E> 的模版*/
template <typename T,
        template<typename, typename> class N = Node /* 重点! */ >
class NodeOperator {

public:

/*
    *    这个时候typename的作用就是告诉c++编译器,typename后面的字符串为一个类型名称,而不是成员函数或者成员变量,
    *    这个时候如果前面没有typename,编译器没有任何办法知道T::LengthType是一个类型还是一个成员名称(静态数据成员或者静态函数),所以编译不能够通过。
    */

    //typedef typename trait::type_addition<T>::value_type value_type_n;    //---实例化int模版        typename 不可以省略

    typedef typename trait::type_addition<int>::value_type value_type_n;  //---实例化int模版  typename 可以省略

    typedef typename trait::type_addition<void>::value_type value_type_v;   //---实例化void模版

    typedef typename trait::type_addition<char>::value_type value_type_c;      //---通用模版
    typedef typename trait::type_addition<char*>::value_type value_type_pc;                //---实例化T*模版
    typedef typename trait::type_addition<char&>::value_type value_type_rc;                //---实例化T&模版
    typedef typename trait::type_addition<const char*>::value_type value_type_cpc;        //---实例化const T*模版
    typedef N<T, value_type_n> node_type_n; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */
    typedef N<T, value_type_v> node_type_v; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */
    typedef N<T, value_type_c> node_type_c; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */
    typedef N<T, value_type_pc> node_type_pc; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */
    typedef N<T, value_type_rc> node_type_rc; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */
    typedef N<T, value_type_cpc> node_type_cpc; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */

    //node_type_n node;
    //node_type_v node;
    node_type_c node;
    // node_type_pc node;
    // node_type_rc node;
    // node_type_cpc node;
};
 
/*
 *
 */
//
//程序入口点。
//
int main()
{

    NodeOperator<char> no1;
    no1.node.sayHello();
 
    NodeOperator<int> no2;
    no2.node.sayHello();

    return 0;

}


结果:

hello ! T=c E=v
hello ! T=i E=v
huyf@ubuntu:~/test$ g++ t1.cpp t2.cpp -o t12
huyf@ubuntu:~/test$ ./t12
hello ! T=c E=i
hello int !
huyf@ubuntu:~/test$ g++ t1.cpp t2.cpp -o t12
huyf@ubuntu:~/test$ ./t12
hello ! T=c E=v
hello ! T=i E=v
huyf@ubuntu:~/test$ g++ t1.cpp t2.cpp -o t12
huyf@ubuntu:~/test$ ./t12
hello ! T=c E=c
hello <int, char> !
huyf@ubuntu:~/test$


猜你喜欢

转载自blog.csdn.net/windgs_yf/article/details/80927806