c++ - 在从未实例化的模板中错误使用非相关名称是否需要诊断?

标签 c++ templates language-lawyer dependent-name

这是标准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/

相关文章:

C++ : operator<< fails to compile

符合标准的字符串是否可以超过 SIZE_MAX 个字符?

c++ - 调用函数指针时出错

c++ - 在加权树中,偏心率最小的顶点必须始终是树的根吗?

C++ 套接字连接错误

c++ - 如何定义 is_instantiable 类型特征?

c++ - 编译类型模板谓词使用 Clang 进行编译,但不使用 GCC 或 MSVC

android - 使用 Xamarin 的 NDK Android 应用程序中的 DllNotFoundException

C++:使用模板折叠具有不同类型和功能的重复代码

c++ - 在不使用变量的情况下实例化 ifstream 对象