I want to compare sequence numbers (given to this code from elsewhere) that may wrap around. Simply comparing two such values will not handle the case like 0x00000002 being greater than 0xfffffffd, but I can assume that the numbers being compared are close together, not nearly 4 billion apart.I get the desired result efficently if, rather than comparing, I subtract, allowing two's complement wrap-around, and then interpreting the difference as signed.  In fact, I would hope that the final test (for branching) against 0 will be no extra cost as the subtraction already set the flags.My question concerns the presence of undefined behavior in C++11 and later.  How do I write this so it does what I said, rather than the optimizer possibly trashing the code, perhaps assuming that the result is never negative.The straightforward code would be:bool seq_ordered (uint32_t a, uint32_t b){auto delta = int32_t (b-a);return delta > 0;}Would it be better to do the initial subtraction while interpreting the arguments as signed or unsigned? Is there a modern idiom to provide for wrapping arithmetic?
On a modern compiler, how do I intentionally code for 2s complement with wraparound?
c++;undefined behavior