views:

135

answers:

1

Hi, I'm trying really hard to made this work, but I'm having no luck. I'm sure there is a work around, but I haven't run across it yet. Alright, let's see if I can describe the problem and the needs simply enough:

I have a RGB template class that can take typenames as one of its template parameters. It takes the typename and sends it into another template that creates a classification of its basic type. For example:

struct float_type {};
struct bit_type {};
struct fixed_pt_type {};

template <typename T> struct type_specification { typedef float_type type; };

template <> struct type_specification<char>      { typedef bit_type type; };
template <> struct type_specification<short>     { typedef bit_type type; };
template <> struct type_specification<int>       { typedef bit_type type; };
template <> struct type_specification<long>      { typedef bit_type type; };
template <> struct type_specification<long long> { typedef bit_type type; };

Then with this, I have a template that calculates Max Values for each of the RGB values based on its bit count:

template <int Bits, typename T> struct Max_Values { enum { MaxValue = (1 << Bits) - 1; }; };
template <int Bits> struct MaxValues<float_type>  { enum { MaxValue = 1.0; }; };

Then in the actual RGB template class, I have:

enum
{
     RMax = Max_Values<RBits, type_specification<T>::type>::MaxValue;
     GMax = Max_Values<GBits, type_specification<T>::type>::MaxValue;
     BMax = Max_Values<BBits, type_specification<T>::type>::MaxValue;
};

This works really well for me, until I got into the fixed-pt needs. The max value is a bit different and I don't know how to create a type-specification specialization to isolate it out. The only work around I have is the process of elimination and creating specializations for float and double and assuming the general case will be fixed-pt. But there has to be a better way to do this. Here is what I want to do with incorrect code:

template <> struct type_specification<fixed_pt_t> { typedef fixed_pt_type type; };

However, fixed-pt-t is a template class that looks like:

template <int N, typename T, template <class> class Policy> struct fixed_pt_t

So the compiler does not like the specialization without template parameters.
Is there a way to specialize my type-specification class to work with fixed-pt-t?
It works fine for the general case, just can't isolate it.

+3  A: 

Perhaps I'm missing some bigger complication, but is there any reason why you can't just partially specialize the type_specification template?

Something like this:

template <int N, typename T, template <class> class Policy>
struct type_specification< fixed_pt_t<N, T, Policy> >
{
    typedef fixed_pt_type type;
};
Charles Bailey
You know, I actually tried that, but it never ran the code. I assumed that because the max-values<Bits, type-specification<T>::type>::value was running with a single typename T (fixed-pt-t in this case) it used the simple specialization.
Brenton
Oh boo. You know what. It did work. I just didn't have the equation for max values setup properly, so I thought it wasn't running the code. I've been banging my head in the wall for a couple of days now!Thanks, your solution worked great!
Brenton
OK, that's all right then, I was just knocking up a quick test app to check my sanity. I'd just like to point out that you're missing the keyword `struct` in quite a few places in your question. Perhaps you'd like to edit to make it a bit more compilable.
Charles Bailey
Yep, I added all the struct keywords up there so its more compilable. I can't wait until template metaprogramming is easier to debug!
Brenton