通过依赖类型使用非类型模板参数对单类型模板参数类模板进行部分专业化 [英] Partial specialization of single type template parameter class template using non-type template parameter through a dependent type

查看:45
本文介绍了通过依赖类型使用非类型模板参数对单类型模板参数类模板进行部分专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下所有标准参考文献均引用 N4659:Kona后,2017年3月工作草案/C ++ 17 DIS .

请考虑以下代码段:

#include <type_traits>

template <int N> struct num {};

template <typename> struct A;

// (1)
template <int N> struct A<num<N>> { using type = bool; };

// (2)
template <long N> struct A<num<N>> { using type = char; };

static_assert(!std::is_same_v<long, int>, "");

// (A)
static_assert(std::is_same_v<A<num<1>>::type, bool>, "");

int main() {}

(A)处的 static_assert 对于GCC是成功的,但对于Clang是失败的:

The static_assert at (A) is successful for GCC, but fails for Clang:

error: static_assert failed due to 
       requirement 'std::is_same_v<char, bool>' ""

本质上,GCC选择完全匹配的专业化(1),而Clang选择专业化的专业化(2).

Essentially, GCC picks the perfectly matching specialization (1), whereas Clang picks the specialization (2).

类似地,如果我们删除断言和专门化(1):

Similarly, if we remove the assertions as well as specialization (1):

template <int N> struct num {};

template <typename> struct A;

// (2)
template <long N> struct A<num<N>> { using type = char; };

int main() {
  A<num<1>> a{};
  (void)a;
}

然后GCC无法编译程序,而Clang接受了该程序.

Then GCC fails to compile the program whereas Clang accepts it.

海湾合作委员会:

GCC:

error: variable '`A<num<1> > a`' has initializer but incomplete type

此行为适用于各种GCC和Clang版本,以及适用于这些版本的各种C ++语言级别(C ++ 11,C ++ 14,C ++ 17,C ++ 2a).

This behaviour holds over various GCC and Clang versions, as well as various C++ language levels over these version (C++11, C++14, C++17, C++2a).

  • 上面的第一个代码片段实际上是否格式错误(不需要诊断?),还是GCC或Clang错误?

我的猜测是,这是错误的形式,但尚未能够应用 [temp.class.spec]/8.1 ?

My guess is that this is ill-formed, but haven't been able to apply a relevant part of [temp.class.spec] to reject it. Perhaps [temp.class.spec]/8.1?

[temp.class.spec]/8.1 与专门的非类型参数相对应的模板参数的类型不应取决于专门化的参数.[示例:[...] —示例]

[temp.class.spec]/8.1 The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. [ Example: [...] — end example ]

推荐答案

据我所知,第一个代码段格式不正确(需要诊断 );编译器应该由于部分专业化(2)而拒绝该程序.

As far as I can tell, the first snippet is ill-formed (and a diagnostic is required); compilers should reject the program because of the partial specialization (2).

[temp.deduct.type]/18 在这里适用:

如果 P 具有包含< i> 的形式,并且 i 的类型不同从模板的相应模板参数的类型由封闭的 simple-template-id 命名,推论失败.[...]

If P has a form that contains <i>, and if the type of i differs from the type of the corresponding template parameter of the template named by the enclosing simple-template-id, deduction fails. [...]

标准中的关联示例使用功能模板,但在其他方面非常相似.

The associated example in the Standard uses a function template, but is otherwise very similar.

因此永远无法推导部分专业化(2)的模板参数,并且

So the template argument of the partial specialization (2) can never be deduced, and [temp.class.spec.match]/3 applies:

如果部分专业化的模板参数不能为归因于其 template-parameter-list 的结构和 template-id ,程序格式错误.

If the template arguments of a partial specialization cannot be deduced because of the structure of its template-parameter-list and the template-id, the program is ill-formed.


有趣的是,我找不到能诊断此问题的编译器,甚至找不到严格模式下的EDG.我们可以推测,大多数编译器作者认为在这里进行诊断不值得为执行检查付出努力.这可能意味着将来我们可能会看到以上段落中的要求从病态变为病态,无需诊断.但是,这纯粹是猜测.无论如何,我认为它永远不会变成格式良好;我想不出永远无法匹配的部分专业化的有效用途.


Interestingly, I couldn't find a compiler that diagnoses this issue, not even EDG in strict mode. We could speculate that most compiler writers consider the benefits of having a diagnostic here not to be worth the effort of implementing the checks. This could mean that we might see the requirement in the paragraph above change in the future from ill-formed to ill-formed, no diagnostic required. However, this is pure speculation. In any case, I don't see it ever changing to well-formed; I can't think of a valid use for a partial specialization that never matches.

[temp.deduct.type]/18 通过 CWG2091的分辨率进行了澄清.

这篇关于通过依赖类型使用非类型模板参数对单类型模板参数类模板进行部分专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆