通过使用类型特征,我可以发现一个类型是整数还是一个指针(以及更多)。是否也可以找出传递的指针是否是整型数据类型(int,
float
,char)而不是对象?我可以通过类型特征确定指针是否为整型:
编辑:除了Armen's答案,如果有人使用洛基库而不是加速的remove pointer
功能类似于TypeTraits::PointeeType
通过使用类型特征,我可以发现一个类型是整数还是一个指针(以及更多)。是否也可以找出传递的指针是否是整型数据类型(int,
float
,char)而不是对象?我可以通过类型特征确定指针是否为整型:
编辑:除了Armen's答案,如果有人使用洛基库而不是加速的remove pointer
功能类似于TypeTraits::PointeeType
boost::is_pointer<T>::value &&
boost::is_integral<boost::remove_pointer<T>::type>::value
顺便说一句float
组是不完整。您可能需要is_arithmetic
如果您无法访问Boost(或者如果Boost实现使用很多您不想去的机器),您可以从std :: numeric_limits
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 ...
我不认为这回答了这个问题。问题是:如何确定T是否是指向* integral *类型的指针?你的特征告诉我T是否是一般指针。 –
你说得对,我误读了。 –
'整型数据类型(int,float,char)'?浮动也是不可或缺的? – Nawaz
浮点数不是整数,并且int,float和char类型的对象都是,也是对象。 – jalf