Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does *&++i cause undefined behaviour in C++03?

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.

like image 251
M.M Avatar asked Feb 11 '15 21:02

M.M


People also ask

What do we mean by does?

Definition of doespresent tense third-person singular of do.

Does meaning and examples?

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.

Why we use 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.

What type of word is do?

do auxiliary verb uses. Word forms: does, doing, did, donelanguage note: Do is used as an auxiliary with the simple present tense.


1 Answers

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…

like image 66
Cheers and hth. - Alf Avatar answered Oct 13 '22 01:10

Cheers and hth. - Alf