为什么即使直到最后才定义实际类型,依附名称也可以视为完整名称 [英] Why can a dependent name be considered as complete even if the actual type is not defined until the very end
问题描述
template <class T>
void Yeap(T);
int main() {
Yeap(0);
return 0;
}
template <class T>
void YeapImpl();
struct X;
template <class T>
void Yeap(T) {
YeapImpl<X>(); // pass X to another template
}
template <class T>
void YeapImpl() {
T().foo();
}
struct X {
void foo() {}
};
请注意,直到定义$
Note that struct X
is not defined until the very end. I used to believe that all odr-used names must be complete at the point of the instantiation. But here, how can the compiler treat it as a complete type prior to its definition?
我已经检查了依赖关系的绑定规则和查找规则以及相关的函数模板实例化。
I have checked the binding rules and lookup rules of dependent name and function template instantiation in cppreference, but none of them can explain what is happening here.
推荐答案
我相信该程序格式不正确,不需要诊断。
I believe this program is ill-formed, no diagnostic required.
[temp.point ] / 8 读取,并删除不相关的部分:
[temp.point]/8 reads, editing out the irrelevant parts:
功能模板的专业化可能[...]翻译单元中有多个实例化点,除了上述实例化点之外,对于在翻译单元中具有实例化点的任何此类特殊化,翻译单元的末尾也被视为实例化点。 [...]如果根据一个定义规则,两个不同的实例化点赋予模板专业化不同的含义,则程序格式错误,无需诊断。
A specialization for a function template [...] may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. [...] If two different points of instantiation give a template specialization different meanings according to the one-definition rule, the program is ill-formed, no diagnostic required.
YeapImpl< X>
有两个实例化点:在问题的注释行和翻译结束时调用单元。在实例化的第一点, X
是不完整的,这会使函数的主体格式不正确。在实例化的第二点中, X
是完整的,这使主体结构良好。
YeapImpl<X>
has two points of instantiation: where it is called on the commented line in the question and at the end of the translation unit. In the first point of instantiation, X
is incomplete which would make the body of the function ill-formed. In the second point of instantiation, X
is complete which makes the body well-formed.
这两个专业有[非常]不同的含义。
Those two specializations have [very] different meanings.
这篇关于为什么即使直到最后才定义实际类型,依附名称也可以视为完整名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!