模板专业化问题

发布于 2024-08-10 08:50:18 字数 1842 浏览 9 评论 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;
};

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-08-17 08:50:18

也许我错过了一些更大的复杂性,但是有什么理由不能部分专门化 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;
};

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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文