I have a pointer int* p, and do some operations in a loop. I do not modify the memory, just read. If I add const to the pointer (both cases, const int* p, and int* const p), can it help a compiler to optimize the code?
I know other merits of const, like safety or self-documentation, I ask about this particular case. Rephrasing the question: can const give the compiler any useful (for optimization) information, ever?
While this is obviously specific to the implementation, it is hard to see how changing a pointer from
int*toint const*could ever provide any additional information that the compiler would not otherwise have known.In both cases, the value pointed to can change during the execution of the loop.
Therefore it probably will not help the compiler optimize the code.