首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::is_integral是如何实现的?

std::is_integral是如何实现的?
EN

Stack Overflow用户
提问于 2017-04-23 13:46:24
回答 1查看 5K关注 0票数 13

我不熟悉cpp中的模板魔术。在阅读了“TemplateRex”在链接中说的话之后,我对std::is_intergral的工作方式感到困惑。

代码语言:javascript
复制
template< class T >
struct is_integral
{
    static const bool value /* = true if T is integral, false otherwise */;
    typedef std::integral_constant<bool, value> type;
};

我能理解SFINAE是如何工作的以及特征是如何工作的。在引用优先选择之后,找到了“is_pointer”的实现,而不是“is_integral”的实现,该实现如下所示:

代码语言:javascript
复制
template< class T > struct is_pointer_helper     : std::false_type {};
template< class T > struct is_pointer_helper<T*> : std::true_type {};
template< class T > struct is_pointer : is_pointer_helper<typename std::remove_cv<T>::type> {};

“is_integral”有类似的实现吗?多么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-23 13:49:17

我们从这里获得了这样的信息:

检查T是否为整型。提供成员常量值,如果T是boolcharchar16_tchar32_twchar_tshortintlonglong long或任何实现定义的扩展整数类型,包括任何有符号、无符号和cv限定的变量,则该值等于true。否则,值等于false。

这样的事情很可能就是您可以实现它的过程:

代码语言:javascript
复制
template<typename> struct is_integral_base: std::false_type {};

template<> struct is_integral_base<bool>: std::true_type {};
template<> struct is_integral_base<int>: std::true_type {};
template<> struct is_integral_base<short>: std::true_type {};

template<typename T> struct is_integral: is_integral_base<std::remove_cv_t<T>> {};

// ...

请注意,std::false_typestd::true_typestd::integral_constant的专门化。有关更多细节,请参见这里

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43571962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档