4 ms·
What I would like to write in unit tests: for (auto x : y) ok &= check(x); That gets you angry messages from static analysis, so you probably have t
by MauranKilom 5y ago
What I would like to write in unit tests:
for (auto x : y)
ok &= check(x);
That gets you angry messages from static analysis, so you probably have to write (since there is no corresponding assignment operator)
for (auto x : y)
ok = ok && check(x);
...except now you don't actually perform checks after the first failure. That may or may not be intentional (or even confusing - depending on logging). What you'd really need to write to preserve the original logic is
for (auto x : y)
ok = check(x) && ok;
Now the `&& ok` is much easier to miss (both in writing and reading) and the intent is much less clear.
I really do wonder why the standard doesn't just specify the behavior of bool & bool. Is it just because of holdover from C?
- dataflow 5y ago> I really do wonder why the standard doesn't just specify the behavior of bool & bool. Confused about your question. Did you have a typo? bool & bool is valid in C++.
- thaumasiotes 5y agoI'm guessing the question is related to this complaint: > What I would like to write in unit tests: > ok &= check(x); > That gets you angry messages from static analysis
- dataflow 5y agoThat has nothing to do with the standard though? The behavior is well-defined, it's just compilers warning about use of & tending to be error-prone (rightly or wrongly).
- gpderetta 5y agoI lost track how many times I have been bitten by the ok = ok && ... trap.