Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer range based template specialisation [duplicate]

Tags:

c++

templates

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>
{

};
like image 496
Ram Avatar asked Mar 01 '12 04:03

Ram


2 Answers

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.

like image 115
iammilind Avatar answered Sep 22 '22 04:09

iammilind


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
}
like image 29
Seth Carnegie Avatar answered Sep 22 '22 04:09

Seth Carnegie