基于整数范围的模板专业化 [英] Integer range based template specialisation

查看:47
本文介绍了基于整数范围的模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得一个基于整数值作为模板类型的专业化名称。如果我使用单个整数,则专门化是直接的。是否有可能基于范围而不使用boost基础的简单模板专业化。

I was trying to get a specialisation based on integer value as the template type. If I use a single integer, specialisation is straight forward. Is it possible to have a simple template specialisation based on range without using boost foundation.

这是代表代码,翻译为

template <typename int val>
class Param
{
public:

};

template <> class Param<0 to 100>
{

};

template <> class Param<100 to 175>
{

};


推荐答案

这是一种实现需求的(简单)方法使用 SFINAE

Here is one (simple) way to implement your requirements using SFINAE:

template<bool> struct Range;

template<int val, typename = Range<true> >
class Param
{};

template<int val>
class Param<val, Range<(0 <= val && val <= 100)> >
{};

template<int val>
class Param<val, Range<(100 < val && val <= 175)> >
{};

演示

这篇关于基于整数范围的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆