type_traits这样可以吗
以下是源代码
#include<iostream>
#define null template<>
using namespace std;
struct a_type{};
struct b_type{};
struct c_type{};
void output(int i, a_type)
{
cout << i << endl;
}
void output(int *j, b_type)
{
cout << *j << endl;
}
void output(int &k,c_type)
{
cout << k << endl;
}
template<class T>
struct type_traits
{
};
null struct type_traits<int>
{
typedef a_type atype;
};
null struct type_traits<int *>
{
typedef b_type btype;
};
null struct type_traits<int &>
{
typedef c_type ctype;
};
int main()
{
int t = 2;
int *i;
i = &t;
int &d = t;
cout << t << endl;
cout << d << endl;
typedef type_traits<int>::atype aatype;
typedef type_traits<int *>::btype bbtype;
typedef type_traits<int &>::ctype cctype;
output(t, aatype());
output(i, bbtype());
output(d, cctype());
return 0;
}
结果如下:
2
2
2
2
2