17

The new standard has different undefined behavior from the old one. The new sequencing rules, for example, mean that some arithmetic operations that used to be undefined (for such reasons as multiple writes between sequence points) are now defined.

So, what do we need to learn anew about undefined behavior?

Destructor
  • 14,123
  • 11
  • 61
  • 126
David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • 10
    Well, first of all...undefined behavior in C++0x doesn't cause nasal demons...it summons butt monkeys. – Edward Strange Dec 07 '10 at 17:28
  • 2
    I hardly think this is worth learning. Those expressions are still not human readable. – UncleBens Dec 07 '10 at 17:30
  • 1
    @UncleBens: which part of "for example" wasn't in a large enough font for you? ;-) – Steve Jessop Dec 07 '10 at 17:37
  • 2
    This SAQ(Self Answered Question) might be useful for you : http://stackoverflow.com/q/4176328/ – Prasoon Saurav Dec 07 '10 at 18:00
  • @Rambo: note that in C++0X the concept of sequence point has been abandoned and replaced with the trickier concepts of sequenced-before and sequenced-after. Also now the value computation part is conceptually separate from side effects... all this for example makes `i=++i+1` legal but `i=i++ + 1` illegal. However AFAIK what was legal before is legal also in C++0X so playing by the old simpler rules is a safe option. – 6502 Dec 09 '10 at 09:23

2 Answers2

14

In my opinion the new rules are more complex to describe and to understand. For example consider that:

int x = 12;
x = x++ + 1; // undefined behaviour
x = ++x + 1; // valid

I would suggest to simply avoiding multiple side effects to the same variable in the same expression that is a rule simpler to understand. AFAIK C++0X changed some cases that were undefined behaviour in the past and that are now legal uses (for example the second of the two expressions above) but remember that there is and there will always be a difference between what is legal and what is moral ;-) ... no one is forcing you to use such things.

Actually in the above case seems that the validity of the second expression happened unintentionally as a side effect of fixing another issue (#222) in the language. The decision was to make the expression valid because it was considered that changing something from UB to well defined wasn't going to do any harm. I think however that while this didn't make any damage to programs (where of course UB is the worst possible problem), it actually made some damage to the language itself... changing a rule that was already complex to explain and understand to an even more obscure one.

IMO C++ is continuing its natural evolution from C into a language where a bunch of good-looking nice and logical statements can do wonderful things... and in which another bunch of equally good-looking, equally nice and equally logical statements can make your computer to explode instead.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 3
    Does that mean that `+++++++++++++++++++i` is a valid expression now? – Edward Strange Dec 07 '10 at 17:48
  • Yes... in C++0X it's valid. Yes I agree this sucks. And thanks for the downvotes... – 6502 Dec 07 '10 at 17:56
  • 2
    Since when was C nice looking or logical? C++ ugly semantics derive from design faults in C, and even worse faults in core C++. This isn't evolution: the type system was screwed from the start. It's just now that some people are trying to patch up the consequences what was intrinsically flawed is now becoming more evident. – Yttrill Dec 07 '10 at 19:19
  • 1
    +1 "there will always be a difference between what is legal and what is moral" – Stephane Rolland Jan 01 '11 at 12:54
3

C++0x changes a number of previously undefined cases to now conditionally-supported cases. The semantics is:

  • If the implementation does not support the conditionally-supported feature, it shall document that and shall emit a diagnostic for a program that violates it.
  • If the implementation does support it, it should behave to additional requirements the Standard makes on it. For example, the Standard might say something is conditionally-supported with implementation-defined semantics. If so, the implementation shall document how it supports the feature.

A popular case that previously was undefined is when passing an argument of class type having a non-trivial copy constructor, a non-trivial move contructor, or a non-trivial destructor though an ellipsis function parameter. This is now conditionally-supported, with implementation-defined semantics.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • I wish C would add such concepts. Out of curiosity, does C++ define a category of programs that rely upon conditionally-supported behaviors but will behave correctly on implementations that support all requirements (much tighter than "conforming" but much looser than "strictly-conforming"?) – supercat Aug 10 '16 at 21:47