4 ms·
How about: #define ADD_OR_DEFAULT(a, b, d) \ ({ typeof((a) + (b)) r; __builtin_add_overflow((a), (b), &r) ? (d) : r; }) and then: return ADD_OR_DEF
by Kristine1975 10y ago
How about:
#define ADD_OR_DEFAULT(a, b, d) \
({ typeof((a) + (b)) r; __builtin_add_overflow((a), (b), &r) ? (d) : r; })
and then:
return ADD_OR_DEFAULT(a, b, 0);
P.S: Names beginning with two underscores are reserved for the implementation.
- efaref 10y agoThe problem with solutions like this is that they break the abstraction of using algebraic notation for calculations. We're conditioned to expect calculations to be written in algebraic notation, and indeed one of the original selling points of C is that it lets you express calculations in that form. To undo that is a massive shame. The calculation itself absolutely MUST look like this: a + b While maybe it's not too bad for simple addition, think of compound expressions with multiple terms.