使用模版类作为模版参数

/* 
 * File:   main.cpp
 * Author: Administrator
 *
 */
#include <iostream>

//-------------------

namespace trait {
    template <typename T>
    struct type_init;

    template <>
    struct type_init<int> {

        enum {
            value = 0
        };
    };

    template <>
    struct type_init<unsigned int> {

        enum {
            value = 0
        };
    };

    template <>
    struct type_init<char> {

        enum {
            value = 0
        };
    };

    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;
    };

    template <>
    struct type_addition<void> {
        typedef void value_type;
        typedef void* pointer;
        typedef const void* const_pointer;
    };

    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;
    };

    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;
    };

    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;
    };

}

//-------------------

/**定义模版类*/
template <typename T, typename E>
class Node {
public:

    void sayHello(void) {
        std::cout << "hello !" << std::endl;
    }
};

/**特化模版类*/
template <>
class Node<int, int> {
public:

    void sayHello(void) {
        std::cout << "hello int !" << std::endl;
    }
};

/**创建模版类,其中模版的参数N也是一个模版类,且默认为Node<typanem T,typename E> 的模版*/
template <
        typename T,
        template<typename, typename> class N = Node /** 重点! */
>
class NodeOperator {
public:
    typedef typename trait::type_addition<T>::value_type value_type;
    typedef N<T, value_type> node_type; /** 模版类参数的完全特化,其参数可使用外部模版类的参数 */
    node_type node;
};

/*
 * 
 */
int main(void) {

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

    NodeOperator<int> no2;
    no2.node.sayHello();
    return 0;
}

hello !
hello int !

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值