In another answer it was stated that prior to C++11, where i
is an int
, then use of the expression:
*&++i
caused undefined behaviour. Is this true?
On the other answer there was a little discussion in comments but it seems unconvincing.
Definition of doespresent tense third-person singular of do.
Does references the performance or achievements of another. An example of does is telling a friend that your husband is in marketing, "He does marketing." Plural form of doe. Third-person singular simple present indicative form of do.
Do as an auxiliary verb. Do is one of three auxiliary verbs in English: be, do, have. We use do to make negatives (do + not), to make question forms, and to make the verb more emphatic.
do auxiliary verb uses. Word forms: does, doing, did, donelanguage note: Do is used as an auxiliary with the simple present tense.
It makes little sense to ask whether *&++i
in itself has UB. The deferencing doesn't necessarily access the stored value (prior or new) of i
, as you can see by using this as an initializer expression for a reference. Only if an rvalue conversion is involved (usage in such context) is there any question to discuss at all. And then, since we can use the value of ++i
, we can use the value of *&++i
with exactly the same caveats as for ++i
.
The original question concerned essentially i = ++i
, which is the same as i = *&++i
. That was undefined behavior in C++03, due to i
being modified twice between sequence points, and is well-defined in C++11, due to the side-effects of the assignment operator being sequenced after the value computations of the left and right hand sides.
It is perhaps relevant to note that the non-normative examples in the C++98 and C++03 standards, were incorrect, describing some cases of formally Undefined Behavior as merely unspecified behavior. Thus, the intent has not been entirely clear, all the way back. A good rule of thumb is to simply not rely on such obscure corner cases of the language, to avoid them: one should not need to be a language lawyer in order to make sense of the code…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With