c++ - g++ 未报告未实例化模板中的某些错误

标签 c++ templates g++

考虑这个例子:

class A
{
  void foo();
  public:
  void bar();
};

template <class> class B
{
  B()
  {
    A a;
    a.foo();    // 1
    A::bar();   // 2
    a.bar(1);   // 3
  }
};

注意 B 永远不会被实例化。

clang++ 将所有三个标记的行报告为错误。 g++ (4.8.3) 接受行 12 并且只报告行 3

如果 B 被实例化,g++ 会愉快地报告所有三行都是错误的。

这是 g++ 错误吗?会有人这么想。 A 不是从属名称,它的成员应该在模板定义时正常检查。有没有我看不到的细微差别?

最佳答案

这些预实例化消息不是标准强制执行的,而是取决于编译器

n3337 § 14.6 - 8

No diagnostic shall be issued for a template definition for which a valid specialization can be generated. If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required.

强调我的

关于c++ - g++ 未报告未实例化模板中的某些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126705/

相关文章:

Eclipse IDE 中的 C++ 错误 'nullptr was not declared in this scope'

c++ - 了解 Valgrind 输出

c++ - Variadic 模板包装函数调用

c++ - 模板类,函数特化

c++ - 用模板参数替换常量是不是更好?

C++:如何通过删除多个元素来删除 "set"?

对象与对象引用的 C++ 类型错误

c++ - 如何有效地将二进制文件读入 vector C++

c++ - 使用随机数生成器为随机数生成器池播种

c++ - WIN32, C++ : Is it possible to animate a window without hiding it?