J
James Kuyper
[...]
It was only labeled as a warning; it was not a mandatory diagnostic. A
compiler can choose to warn you about anything it wants. It could warn
you not to compile programs on Friday the 13th.
But nobody does, except for an Easter egg.
Why is that? Is it because they want to
stay in the compiler writing business?
The fact that a warning
was issued is not relevant to the question of whether or not this code
has defined behavior.
It is a question---a question that two people evaded.
Why have the warning at all?
Because it's risky code. It's not undefined in this case, but the safety
of this code is fragile, and in many cases cannot be checked at compile
time - what if foo() were called with the address of an object that WAS
declared const, in some other translation unit? The problem caused by
such a call would be detectable no earlier than link time, and many C
compilers would be unable to determine whether or not it was safe.
Giving a warning is a lot easier than tracking down whether the code was
actually safe.