我们可以给一个格式不正确的特化命名,只要我们不实例化它。

huangapple go评论75阅读模式
英文:

Can we name an ill-formed specialization as long as we don't instantiate it?

问题

This code is ill-formed, no diagnostic required.

英文:
template <typename T, int N>
struct array {
    T data[N];
};

using bad_array = array<void, 0>;

In the above code, we are referring to a specialization of the array class template which is not instantiated. If it was instantiated, then the program would be ill-formed, because we would generate a void data[0] member.

Is this code ill-formed, no diagnostic required, or is it well-formed?

答案1

得分: 4

是的,它是格式良好的。我无法提供标准参考,因为它根本不违反其中任何规定。

没有规定类型别名的形成需要类型是完整的,也没有规定类型的完整性会影响别名声明的行为,因此根据temp.inst/2没有隐式实例化。因此,特定特化的实例化是否不正确无关紧要,直到您以某种其他方式隐式或显式实例化它。

而且,由于temp.res.general/8列出的项目中也没有适用于您的模板,因此根据temp.res.general/8的最后一句所要求的,编译器不应为模板本身发出任何诊断。

在编写除函数模板之外的任何模板标识符时,无论上下文如何,模板标识符必须是“有效”的,否则程序是不正确的。这方面的要求在temp.names/6中,并包括验证模板参数的数量和种类是否正确,以及对非相关模板标识符满足约束条件,即与模板的声明而不是定义相关的所有与语法有关的要求。但模板标识符array<void, 0>也不违反这些要求。

英文:

Yes, it is well-formed. I can't really provide you a standard reference, because it simply doesn't violate anything in it.

Nothing says that forming a type alias requires the type to be complete, nor does completeness of the type affect the behavior of the alias declaration and so there is no implicit instantiation according to [temp.inst]/2. So, that the instantiation of the particular specialization would be ill-formed is irrelevant until you instantiate it implicitly or explicit in some other way.

And since also none of the items listed in [temp.res.general]/8 apply to your template, the compiler shall not issue any diagnostics for the template itself as required by the last sentence of [temp.res.general]/8.

When writing a template-id of anything but a function template, then regardless of context, the template-id must be valid, otherwise the program is ill-formed. The requirements for this are in [temp.names]/6 and include verifying that the number and kinds of template arguments are correct as well as that constraints are satisfied for non-dependent template-ids, i.e. all the syntactical requirements connected to the declaration of the template rather than its definition. But none of these requirements are violated by the template-id array<void, 0> either.

huangapple
  • 本文由 发表于 2023年6月15日 03:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定