$5.2.11/7 - "[Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1). ]"
The wordings of this section (C++03) are surprising to me. What is suprising are two things.
a) Firstly, the use of 'may'. Why is it 'may'? Other places in the Standard are very definitive about the undefined behavior
b) Why is that the casting away the constness of a originally const object not straight away 'undefined behavior'. Why is it that a write is required for UB to be triggered?
a) Firstly, the use of 'may'. Why is it 'may'? Other places in the Standard are very definitive about the undefined behavior
Don't look too deeply into the use of the word may here. The point is, casting away constness in this case causes undefined behavior.
The C++ standard uses "may" or "might" often, as in:
1.3.12: Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior.
Emphasis mine. Basically, the standard uses the word "may" as in "is allowed to".
b) Why is that the casting away the constness of a originally const object not straight away 'undefined behavior'. Why is it that a write is required for UB to be triggered?
A write triggers UB because it's possible that const objects can be stored in read-only memory on certain platforms.