4 ms·
A fun fact about sequence points. C++ has switched from "sequence points" to "sequenced before/sequenced after" relation. And it is not really "all" previous si
by praptak 13y ago
A fun fact about sequence points. C++ has switched from "sequence points" to "sequenced before/sequenced after" relation. And it is not really "all" previous side-effects that are guaranteed to be visible after the sequence point but only those associated with the sequence point.
In 'foo(b++, a++ && (a+b));' the sequence point introduced by the '&&' only makes the 'a' see the effect 'a++' but the 'b' might not see the 'b++' (function args are not sequenced in C nor C++).
- ridiculous_fish 13y agoRight! In fact I believe the above code invokes undefined behavior, because b may be accessed for a reason other than to determine its new value, without an intervening sequence point. Put another way, the compiler can arbitrarily interleave the subexpressions within separate arguments: foo( a() && b(), c() && d() ); The compiler is free to invoke these functions in the order a(), c(), d(), b() if it pleases.
- coldpie 13y agoWhy on Earth are you writing a statement like that in the first place?
- ben0x539 13y agoIt's probably less about writing the statement while cackling maniacally and more about recognizing that pattern in less obvious examples in existing code while reviewing it or looking for a bug, and realizing that it might be worth investigating and not just a style nitpick.
- praptak 13y agoExaggerated example to show a pattern. A (hopefully) less outrageous (but still bad!) example of the dangers of sequencing: foo(std::unique_ptr<T1>{new T1 }, std::unique_ptr<T2>{new T2 }); This one's from GotW #102.
- alok-g 13y agoThanks. Just read the GotW. http://herbsutter.com/gotw/_102/ http://herbsutter.com/gotw/_102/
- blt 13y agoI wish ++ didn't exist. It's a waste of mental energy.