0

我正在努力完成这项工作,但我没有运气。我确信有一个解决方法,但我还没有遇到它。好吧,让我们看看我是否可以简单地描述问题和需求:

我有一个 RGB 模板类,它可以将类型名作为其模板参数之一。它获取类型名并将其发送到另一个模板,该模板创建其基本类型的分类。例如:

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; };

然后有了这个,我有一个模板,可以根据每个 RGB 值的位数计算最大值:

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

然后在实际的 RGB 模板类中,我有:

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;
};

这对我来说非常有效,直到我满足了固定点的需求。最大值有点不同,我不知道如何创建类型规范专业化来隔离它。我唯一的解决方法是消除和创建浮点和双精度的专门化过程,并假设一般情况是固定点。但必须有更好的方法来做到这一点。这是我想要对不正确的代码执行的操作:

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

然而,fixed-pt-t 是一个模板类,看起来像:

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

所以编译器不喜欢没有模板参数的特化。
有没有办法专门化我的类型规范类来使用fixed-pt-t?
它适用于一般情况,只是无法隔离它。

4

1 回答 1

3

也许我错过了一些更大的复杂性,但是有什么理由不能让你只对type_specification模板进行部分专业化?

像这样的东西:

template <int N, typename T, template <class> class Policy>
struct type_specification< fixed_pt_t<N, T, Policy> >
{
    typedef fixed_pt_type type;
};
于 2009-11-02T20:35:33.597 回答