2011-09-05 52 views
4

通过使用类型特征,我可以发现一个类型是整数还是一个指针(以及更多)。是否也可以找出传递的指针是否是整型数据类型(int, float ,char)而不是对象?我可以通过类型特征确定指针是否为整型:

编辑:除了Armen's答案,如果有人使用洛基库而不是加速的remove pointer功能类似于TypeTraits::PointeeType

+0

'整型数据类型(int,float,char)'?浮动也是不可或缺的? – Nawaz

+1

浮点数不是整数,并且int,float和char类型的对象都是,也是对象。 – jalf

回答

4
boost::is_pointer<T>::value && 
boost::is_integral<boost::remove_pointer<T>::type>::value 

顺便说一句float组是不完整。您可能需要is_arithmetic

+1

如果您无法访问Boost(或者如果Boost实现使用很多您不想去的机器),您可以从std :: numeric_limits :: is_integer'开始并向外运行。也就是说,从某种程度上来说,避免Boost中的一些事情,在这个答案中使用的东西并不是真正的东西,因为它们即将在C++ 0x中出现。 –

0
template <typename T> 
struct IsPointerToInt { 
    static const bool value = false; 
}; 

template <> 
struct IsPointerToInt<int *> { 
    static const bool value = true; 
}; 

// ... other specializations for the types you are interested in ... 
+1

我不认为这回答了这个问题。问题是:如何确定T是否是指向* integral *类型的指针?你的特征告诉我T是否是一般指针。 –

+0

你说得对,我误读了。 –

相关问题