BINGO!!! The pointer is constant, not the item being pointed to. Since you’re dereferencing the pointer, i.e. accessing the non-const bits, the compiler must reload.
const int* const
instead of const int*Maybe the compiler will happily let you modify the dereferenced const int* (undefined behavior), wouldn’t try it now, but that’s not what the signature promises.
Edit: Thought about it more and read some other comments. Now it makes sense.
Indirection in C and C++ is a mess, but at least C has the "restrict" keyword. Best to program with value types whenever you can, and use pointers and references when you must.
My understanding is that compilers can always assume that there is no other thread involved, which (part of) why C11 atomics are necessary. Is that not the case?
It is allowed to concurrently read from const regions. But not concurrently read and write, and definitely not concurrently write.
void foo(const int* const p1, int* const p2) {
int a = *p1;
*p2 += 42;
int b = *p1;
return a == b; // b = a + 42
}
int main() {
int x = 0;
return foo(&x, &x);
}