2 ms·
What do you do if you really do intend the assignment? (Assume for the sake of argument it’s some more reasonable example like "if (x = re_match(foo, bar)) {..
by iainmerrick 8y ago
What do you do if you really do intend the assignment?
(Assume for the sake of argument it’s some more reasonable example like "if (x = re_match(foo, bar)) {...}")
- 613style 8y agoYou wrap it in parens to avoid the warning: if ((x = 7)) { ... }
- Tyr42 8y agoYou do "if (((x = re_match(foo, bar))) {...}". At least the systems I've seen need a extra (()) around = to disable the warnings and notify the reader that you're not just comparing for equality.
- thestoicattack 8y agoC++17 has if-statement initializers, so you can do if (auto x = re_match(foo, bar); x.has_value()) { (or x.ok() or whatever the name is of the method that tells you the validity of x)