【问题标题】:Why is const ignored at "the outermost level of the parameter type"?为什么 const 在“参数类型的最外层”被忽略?
【发布时间】:2021-05-30 19:37:18
【问题描述】:

我读了一本教科书,遇到了这一段:

非类型模板参数的声明方式与变量非常相似,但它们不能具有静态、可变等非类型说明符。它们可以有 const 和 volatile 限定符,但如果这样 限定符出现在参数类型的最外层,它被简单地忽略:

template<int const length> class Buffer; // const is useless here
template<int length> class Buffer; // same as previous declaration

我真的不明白这是什么意思如果这样 限定符出现在参数类型的最外层,它被简单地忽略。 也就是说,在我们指定参数的模板中,设置const没有意义吗?但我想我之前已经看过很多次了。还是暗示其他什么?

【问题讨论】:

    标签: c++


    【解决方案1】:

    值模板参数只能是编译时常量。不能使用变量或其他运行时数据。所以const 是不需要的,它的值必须已经是一个常量了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 2012-10-27
      相关资源
      最近更新 更多