News for hackers. Stuff that matters.
Re:My favourite C++ footgun by cratermoon on Tuesday June 22, 2021 @07:20PM (#27598613)
There was a post here not too long ago about how C/C++ compiler writers have consistently taken the wiggle room of "undefined behavior" to do terrible, terrible, damage to otherwise fine code. https://news.ycombinator.com/item?id=27221552
Re:My favourite C++ footgun by saagarjha on Tuesday June 22, 2021 @07:40PM (#27598823)
In this case there is no undefined behavior, the order is just unspecified.
Re:My favourite C++ footgun by cratermoon on Tuesday June 22, 2021 @08:55PM (#27599390)
> unspecified

how is that different from undefined?

Re:My favourite C++ footgun by Kranar on Wednesday June 23, 2021 @12:29AM (#27600798)
Undefined behavior is unbounded in both space and time in terms of program semantics, that is, a program that will engender undefined behavior may do so at any point during the course of program execution (unbounded in time) as well as may be observed within any memory region (unbounded in space). A colloquial way of saying this is that a compiler is free to do anything it wants anytime it wants in the presence of undefined behavior.

Unspecified behavior is bounded both in space and time. Unspecified behavior may only affect program semantics within the statement in which it is engendered and may only be observed within the region of memory that is written to or read from.

It is often said that undefined behavior is invalid, and one can sympathize with that statement as it's commonly repeated, but it's not true. All the standard says about undefined behavior is that the standard imposes no requirements about program behavior, it does not state that the program is invalid or that the program must behave in some kind of invalid manner. Almost all non-trivial C++ programs engender undefined behavior in a manner documented by the compiler and do so in a safe and principled way.

Re:My favourite C++ footgun by saagarjha on Tuesday June 22, 2021 @08:58PM (#27599421)
The existence of undefined behavior indicates that the program is invalid. Unspecified behavior is just something that is not precisely defined.
Re:My favourite C++ footgun by mhh__ on Wednesday June 23, 2021 @01:53AM (#27601211)
In English, that's a reasonable point but within the C++ standard behaviour is declared to be specifically undefined in the text - that is to say, we are telling you, you may write this construct however said construct is not C++
Re:My favourite C++ footgun by rocqua on Wednesday June 23, 2021 @05:31AM (#27602535)
Note that this post was heavily discussed and disputed. The question "is undefined behavior treated correctly?" is rather contentious.

It certainly is the case that most compiler writers have decided this is okay. And I personally tend to agree with them.