【问题标题】:Why no error for unused partial specialisation?为什么未使用的部分专业化没有错误?
【发布时间】:2014-10-19 01:42:11
【问题描述】:

下面Foo的特化永远不会被使用; Clang 会发出警告,而 GCC 不会。

template <typename>
struct Foo                { static const int value = 0; };

template <template <typename...> class C, typename ...Ts>
struct Foo<C<Ts..., int>> { static const int value = 1; };

为什么这不会导致错误?

以下代码演示了这一点:

template <typename...>
struct Bar {};

static_assert(!Foo<Bar<char,double,int>>::value,"");

【问题讨论】:

  • @MarkB 部分特化匹配依赖于模板参数推导,这里的包不会被推导(不是模板参数列表中最后一个模板参数的包扩展是非推导上下文) .
  • 我猜这是因为确定是否可以使用部分专业化通常是不可行的。但我无法证明。
  • @kerrekSB 不,这就是问题所在。我认为 OP 知道专业化是“无法访问的”,OP 会问为什么拥有无法访问的专业化并不是错误。
  • 第 14 条将许多结构标记为格式错误但不需要诊断,这很大程度上是因为诊断它们是不可行的;令人惊讶的是,永远不会使用的部分专业化不是其中之一......
  • @T.C.完整的 sn-p:template&lt;class T&gt; struct foo { template&lt;class Unused=void&gt; typename std::enable_if&lt; !std::is_same&lt;int, T&gt;::value &gt;::type not_an_int() const { std::cout &lt;&lt; "not an int\n"; } }; 这在 C++ 下是非法的,因为根据我对标准的阅读,foo&lt;int&gt;::not_an_int&lt;?&gt;() 没有有效的专业化。现在我看到了他们这样做的原因为什么(为了在将来允许更多、更智能的template 函数检查,以便在实例化之前捕获更多无效代码),但它也确实妨碍了。

标签: c++ c++11 variadic-templates template-specialization


【解决方案1】:

定义某些东西而不使用它不是错误。如果是这样,在每个程序中你#include&lt;vector&gt; 你必须使用vector&lt;bool&gt;

【讨论】:

  • 有趣。也许我也应该声明vec tor &lt;b$$l&gt;:
  • 闲置的东西和不可能使用的东西是有区别的。
猜你喜欢
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多