/*
* 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 !