Is the following allowed:
const int const_array[] = { 42 };
int maybe_inc(bool write, int* array) {
if (write) array[0]++;
return array[0];
}
int main(
If it compiles, then it is allowed. But it does not mean, it is legal.
#include
int main(int argc, char *argv[])
{
const int arr[] = {1, 2, 3};
int* parr = const_cast(arr);
parr[0] = 0;
for(auto& n : arr)
std::cout << n << std::endl;
}
The above code compiles in Ubuntu 20.04 g++ compiler. It also runs without problem. But the above code actually is undefined behaviour.