What is undefined behavior?
The concept of undefined behavior (UB) is shrouded in mystery, complete with arcane warnings for the uninitiated. The Usenet group comp.std.c
warns, "When the compiler encounters (an undefined construct), it is legal for it to make demons fly out of your nose." Launching nuclear missiles and neutering your cat (even if you don't own a cat) have been mentioned in a similar context. One of the tangential goals of this chapter is to demystify UB: while the ultimate goal is to explain the relationship between UB and performance and to show how to take advantage of UB, we cannot do that until we can discuss the concept rationally.
First of all, what is UB in the context of C++ (or any other programming language)? There are specific places in the standard where the words the behavior is undefined or the program is ill-formed are used. The standard further says that if the behavior is undefined, the standard imposes no requirements on the results...