Is there an equivalent to the "for ... else" Python loop in C++?
Python has an interesting
for statement which lets you specify an
In a construct like this one:
for i in foo: if bar(i): break else: baz()
else clause is executed after the
for, but only if the
for terminates normally (not by a
I wondered if there was an equivalent in C++? Can I use
for ... else?
A simpler way to express your actual logic is with
if (std::none_of(std::begin(foo), std::end(foo), bar)) baz();
If the range proposal for C++17 gets accepted, hopefully this will simplify to:
if (std::none_of(foo, bar)) baz();
★ Back to homepage or read more recommendations: