方法一:
利用boost的is_fundamental trait进行判断
#include <boost/type_traits/is_fundamental.hpp>
#include <iostream>
template<typename T, bool=true>
struct foo_impl
{
void do_work()
{
std::cout << "0" << std::endl;
}
};
template<typename T>
struct foo_impl<T,false>
{
void do_work()
{
std::cout << "1" << std::endl;
}
};
template<class T>
struct foo
{
void do_work()
{
foo_impl<T, boost::is_fundamental<T>::value>().do_work();
}
};
int main()
{
foo<int> a; a.do_work();
foo<std::string> b; b.do_work();
}
方法二:
用typeid
http://hi.baidu.com/nicker2010/blog/item/5df6ed0d6e0cf7ff37d122a1.html