c++ - 为什么在这段代码中 S 必须是 const ?
问题描述
为什么下面的代码不能编译?它说这S
必须是const
主要错误之一。
template <int S>
class Array
{
int size;
int items [S];
public:
Array(void) :size(S){}
};
void main()
{
int S= 30;
Array <5+S> array;
}
解决方案
非类型模板参数必须是constexpr
,即它们必须在编译时已知。因此,S
必须声明为constexpr int
。
推荐阅读
- amazon-web-services - 允许非管理员用户访问由 root 用户创建的 AWS 资源
- spring-boot - 我如何在百里香中设置输入标签的默认值?
- ios - 当我们按下主页按钮并使用其他应用程序返回该应用程序后,哪个方法会在 AppDelegate 中触发
- fullcalendar - FullCalendar eventRender 问题 = 未知选项“eventRender”?
- oracle-apex - 清除交互式网格 PopupLOV 搜索历史
- c++ - 如何设计需要在基类中使用派生类成员的类?
- python-3.x - 在 cs50 ide 中使用烧瓶时出现内部服务器错误
- grep - 使用 grep (或类似的东西)添加具有相同结构的额外行
- mongodb - Mongo atlas,对单个数据库的访问受限的用户在连接时看不到它
- angular - Multi select is returning NULL