我能否通过类型特征来确定一个指针是否为整型?

4
通过使用类型特征,我可以找出一个类型是否是整数或指针(以及更多)。是否还可能找出传递的指针是基本数据类型(int,float,char)而不是对象的指针? 编辑:除了Armen's的答案之外,如果有人使用LOKI库而不是Boost,那么remove pointer 的功能类似于TypeTraits :: PointeeType

整数数据类型(int,float,char)?float也是整数吗? - Nawaz
1
浮点数不是整数,而int、float和char类型的对象也同样是对象。 - jalf
2个回答

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

顺便提一句,float 不是整数类型。你可能需要使用 is_arithmetic


1
如果您无法访问Boost(或者如果Boost实现使用了您不想接近的大量机制),则可以从std::numeric_limits<T>::is_integer开始向外扩展。话虽如此,如果有必要避免使用Boost中的某些内容,那么本答案中使用的内容并不是其中真正需要避免的,因为它们即将在C++0x中推出。 - Steve Jessop

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是否为指向整数类型的指针?您的特性只能告诉T是否为指针。 - Armen Tsirunyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接