这两天在看c++ template 和 cpunion写的IDL的代码, 在模板参数中,类型参数可以这样构造:
template_class< type( type1, type2, ... ) > a_class;
比如,可以void( void ), void(), void( int ), 也可以int( void ), string( int )等等,编译器是将它们当作不同的类型的来处理的.
写了如下代码,进行实验:
- #include <iostream>
- typedef void(*fun)(int);
- using namespace std;
- template< typename T >
- struct Base
- {
- void test()
- {
- cout << "Base" << "/t=/t";
- cout << "Base<" << typeid(T).name() << ">" << endl;
- }
- };
- template<>
- struct Base < void >
- {
- void test()
- {
- cout << "Base" << endl;
- }
- };
- template<>
- struct Base < void( int ) >
- {
- void test()
- {
- cout << "Base" << endl;
- }
- };
- template<>
- struct Base < fun >
- {
- void test()
- {
- cout << "Base" << endl;
- }
- };
- template<>
- struct Base < int( string, int, char ) >
- {
- void test()
- {
- cout << "Base" << endl;
- }
- };
- int main(int argc, char* argv[])
- {
- Base< void > b_void;
- Base< void( int ) > b_void_int;
- b_void.test();
- b_void_int.test();
- Base< int( string, int, char ) > b_int;
- Base< fun > b_fun;
- b_int.test();
- b_fun.test();
- Base< Base< void > ( Base < int ( string, int, char ) > ) > b_complex;
- b_complex.test();
- return 0;
- }