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.
This is representative code translates to
template <typename int val>
class Param
{
public:
};
template <> class Param<0 to 100>
{
};
template <> class Param<100 to 175>
{
};
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)> >
{};
Demo.
You can use SFINAE with std::enable_if
to make your own handy compile-time range testing class:
#include <iostream>
#include <type_traits>
using namespace std;
template<int Start, int End, int Val, class Enable = void>
struct crange { };
template<int Start, int End, int Val>
struct crange<Start, End, Val, typename std::enable_if<Val >= Start && Val <= End>::type> {
typedef void enabled;
};
template<int Val, class Enable = void>
class Param {
public:
Param() : value(422) { }
int value;
};
template<int Val> // V VV the range [x, y]
class Param<Val, typename crange<0, 10, Val>::enabled> {
public:
Param() : value(1.32) { }
double value;
};
int main() {
Param<1> pdouble;
Param<50> pint;
cout << pdouble.value << endl; // prints 1.32
cout << pint.value << endl; // prints 422
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With