【问题标题】:Default initialization of unused constant variable template未使用的常量变量模板的默认初始化
【发布时间】:2020-10-06 13:31:38
【问题描述】:

以下program

template<typename = void> 
const int n;

gcc编译成功,但是clang报错:

# error: default initialization of an object of const type 'const int'
const int n;
          ^
            = 0

这是有道理的。

如果使用n,gcc 确实会报错,但即使n 未使用,是否也需要进行诊断?

【问题讨论】:

    标签: c++ language-lawyer uninitialized-constant


    【解决方案1】:

    即使n 未使用,是否也需要诊断?

    没有。适用规则为[temp.res.general]/8

    可以在任何实例化之前检查模板的有效性。

    程序格式错误,不需要诊断,如果:

    • 无法为模板或模板中的 constexpr if 语句的子语句生成有效的特化,并且该模板未实例化,或者
    • ...
    • 由于不依赖于模板参数的构造,紧随其定义的模板的假设实例化将是不正确的,或者
    • ...

    所以两个编译器都符合标准。

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多