I found running the following code (clang 5.0/gcc7.1 with std=c++17) at compiling time, it will report an out of bounds error
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]’
But running a normal function in compiling time, it will not report the error.
constexpr int foo(int i)
{
dummy[1000] = i;
return i;
}
int a[foo(1)]; //force foo run at compile time, no compile error
I am wondering whether this behavior is well-defined that in in literal class constructor it will check the boundary.