I would like to perform range check for a std::array at compile time. Here is an example:
#include <iostream>
#include <array>
void rarelyUsedFunction(const std::array<double, 2>& input)
{
std::cout << input[5] << std::endl;
}
int main()
{
std::array<double, 2> testArray;
rarelyUsedFunction(testArray);
}
If I compile this with g++ there is no warning or error, despite the undefined access to an element which is not in the array. The compiled program just prints some random value.
Is there a compiler option in g++ for a suitable range/boundary check, that is performed during compile time? I know that I can add "-D_GLIBCXX_DEBUG" but this will only perform a check during runtime. If I have a function which is not called very often, this won't be triggered.
I am aware, that such a range check could not be performed in all circumstances, but in the case above, the compiler should be able to spot the problem!?