c++ - 是否明确定义 C++ 将在编译时在文字类构造函数中进行数组边界检查?
问题描述
我发现在编译时运行下面的代码(clang 5.0/gcc7.1 with std=c++17),会报越界错误
int dummy[1];
struct CTest {
constexpr CTest() {
dummy[100] = 1;
}
};
constexpr CTest t; // error: array subscript value ‘100’ is outside the bounds of array ‘dummy’ of type ‘int [1]’
但是在编译的时候运行一个正常的函数,是不会报错的。
constexpr int foo(int i)
{
dummy[1000] = i;
return i;
}
int a[foo(1)]; //force foo run at compile time, no compile error
我想知道这种行为是否定义明确,在文字类构造函数中它将检查边界。
解决方案
推荐阅读
- three.js - Three.js raycaster 与精灵的交集完全偏向左侧
- swift - Swift If 语句数组操作范围
- javascript - 如何将 javascript 对象序列化为包含所有 Class 方法的 json 文件?
- python - 如何在 selenium (Python) 中将密钥发送到没有 id 和/或名称的表单
- c# - 比较 2 个 linq 列表中的 2 个属性并根据比较返回一个布尔值
- tfs - 如何从 VSCode 1.17 连接到 tfs
- android - Twilio 语音通话接听和通话
- java - Spark Java 堆空间
- java - 由于缺少带有 jdk 10 的 tools.jar,maven 下的编译器错误
- python-3.x - 使用 Reportlab 将旋转图像居中