Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

17
  • 10
    That is exceptionally clever. I will also agree that it isn't straight forward and I'll probably just use if conditions when programming C++ that is consumed by others. But that is exactly what I need for my own personal use! :) Commented Jul 15, 2016 at 15:19
  • 14
    @Default Passing iterator pairs rather than containers is both more flexible and idiomatic C++. Commented Jul 15, 2016 at 15:24
  • 8
    @Slava, in general ranges won't reduce the number of algorithms. For example, you still need find_if and find whether they work on ranges or pairs of iterators. (There are a few exceptions, such as for_each and for_each_n). The way to avoid writing new algos for every sneeze is to use different operations with the existing algos, e.g instead of for_each_if embed the condition into the callable passed to for_each, e.g. for_each(first, last, [&](auto& x) { if (cond(x)) f(x); }); Commented Jul 15, 2016 at 15:49
  • 9
    I'm gonna have to agree with the first sentence: The standard for-if solution is much more readable and easier to work with. I think the lambda syntax and the use of a template defined somewhere else just to handle a simple loop would irritate or possibly confuse other devs. You're sacrificing locality and performance for... what? Being able to write something in one line? Commented Jul 15, 2016 at 16:50
  • 47
    Cough @Darkenor, generally "exceptionally clever" programming is to be avoided because it annoys the crap out of everyone else including your future self. Commented Jul 15, 2016 at 17:15