Question

I think the easiest way to ask this question is by first giving the code example (I've made it available on ideone as well: http://ideone.com/OjK2sk):

template <typename IntType, IntType MIN_VAL, IntType MAX_VAL>
struct BoundInt
{
    static constexpr IntType MIN = MIN_VAL;
    static constexpr IntType MAX = MAX_VAL;

    IntType value;
};

template <typename T> struct ConversionTraits;

template <typename T>
struct Value
{
    // Pointless for the sake of this example
    void convert()
    {
        ConversionTraits<T>::convert();
    }

    T value;
};

// this 'implementation' is also pointless for example purposes
struct ConvertImpl
{
    static void convert() { }
};
template <> struct ConversionTraits<int> : public ConvertImpl {};

// This is my problem. How do I partially specialise for something that has
// constants as template parameters.
template <typename IntType>
struct ConversionTraits< BoundInt<IntType, IntType, IntType> >
{
    static void convert() {}
};

int main()
{
    Value<int> intval;
    intval.convert();

    Value<BoundInt<unsigned, 0, 100>> value;
    value.convert();
}

As indicated in the comments, I can't work out how to specialise ConversionTraits for BoundInt. The compiler (gcc 4.7) is telling me that for arguments 2 and 3 of BoundInt, it was expecting a constant of type IntType which makes sense. I have no idea how to perform that specialisation, or if it is even possible.

If it's not possible, is there a different approach I can take?

Was it helpful?

Solution

What about this:

template <typename IntType, IntType MIN_VAL, IntType MAX_VAL>
struct ConversionTraits< BoundInt<IntType, MIN_VAL, MAX_VAL> >
{
    static void convert() {}
};

It may look conterintuitive, because the plain ConversionTraits<> template has only 1 parameter, while the specialization has 3.

But then, template BoundInt<> has three parameters, so if you don't want to specify them, have to use one template argument for each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top