依赖名称的部分专业化

发布于 2025-02-06 18:25:30 字数 529 浏览 2 评论 0原文

我正在构建用于算术操作的模板类型系统,我需要添加一些部分专业,以自动简化。

例如,我有一个struct min

struct unknown_t {};

template<typename f>
struct min {
    using type = unknown_t;
};

代表-f(未知直到已知f)。

我需要表达- ( - f)= f。我尝试了以下操作:

template<typename f>
struct min<typename min<f>::type> {
   using type = f;
};

使用

类模板部分失败的部分专业化包含无法推导的模板参数;

我尝试添加某些type_traits,其他虚拟模板参数,但没有任何效用。

I'm building a template type system for arithmetic operations and I need to add some partial specializations for automatic simplification.

For example, I have a struct min:

struct unknown_t {};

template<typename f>
struct min {
    using type = unknown_t;
};

which represents -f (unknown until f is known).

I need to express that -(-f) = f. I tried this :

template<typename f>
struct min<typename min<f>::type> {
   using type = f;
};

which fails with

class template partial specialization contains a template parameter that cannot be deduced;

I tried to add some type_traits, additional dummy template parameters, but nothing worked.

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

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

发布评论

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

评论(1

嘿哥们儿 2025-02-13 18:25:30

它不能这样工作。考虑min&lt; f&gt; ::类型unknown_t对于任何f(除非您专业化)。因此,您不能从min&lt; f&gt; :: typef中推导f

考虑稍微更改设计,并且专门针对min&lt; f&gt;而不是<而不是<代码> min&lt; f&gt; ::类型:

template<typename f>
struct min<min<f>> {
   using type = f;
};

It cannot work like this. Consider that min<f>::type is unknown_t for any f (unless you specialize it). Hence you cannot deduce f from min<f>::type

Consider to change the design slightly and specialize for min<f> rather than min<f>::type:

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