这是标准says关于模板定义中的非依赖名称:
Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used.
[Example 1:
void g(double); void h(); template<class T> class Z { public: void f() { g(1); // calls g(double) h++; // ill-formed: cannot increment function; this could be diagnosed // either here or at the point of instantiation } }; void g(int); // not in scope at the point of the template definition, not considered for the call g(1)
— end example]
我对
h++;
上的评论感到困惑上面写着“格式错误:......这可以在此处或在实例化时进行诊断”。如果实现选择后者,但没有模板的实例化怎么办?在哪里诊断?这是否意味着这实际上是格式错误的,不需要诊断?
最佳答案
格式错误的 NDR 是格式错误的一个特例。在示例中,类模板的定义Z
显然是格式错误的;是否需要诊断取决于是否实例化 Z
出现在翻译单元中的任何位置,该示例似乎不可知(因为它承认可能存在实例化点)。如果确实存在实例化,则实现可以自由地在定义点或实例化点发出诊断。见 [temp.res.general]/8 .
关于c++ - 在从未实例化的模板中错误使用非相关名称是否需要诊断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64519008/