满足条件的多种类型的模板类专业化

发布于 2024-12-22 03:43:59 字数 366 浏览 3 评论 0原文

如果我有一个模板类,如下所示:

template<typename T>
class Type { /* ... */ };

在不以任何方式修改 Type 的情况下,是否有一种简单的方法可以将其专门用于匹配编译时条件的所有此类类型?例如,如果我想专门针对所有整型类型,我想做这样的事情(只有有效的事情,即):

template<typename T>
class Type<std::enable_if<std::is_integral<T>, T>::type> { /* ... */ };

If I have a template class, like so:

template<typename T>
class Type { /* ... */ };

Without modifying Type in any way, is there a simple way to specialize it for all such types that match a compile-time condition? For example, if I wanted to specialize Type for all integral types, I'd like to do something like this (only something that works, that is):

template<typename T>
class Type<std::enable_if<std::is_integral<T>, T>::type> { /* ... */ };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-12-29 03:43:59

这应该有效:

template<typename T, bool B = std::is_integral<T>::value>
class Type;

// doesn't have to be a specialization, although I think it's more clear this way
template<typename T>
class Type<T, false> { /* ... */ };

template<typename T>
class Type<T, true> { /* ... */ };

This should work:

template<typename T, bool B = std::is_integral<T>::value>
class Type;

// doesn't have to be a specialization, although I think it's more clear this way
template<typename T>
class Type<T, false> { /* ... */ };

template<typename T>
class Type<T, true> { /* ... */ };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文