I have the following method of some class, which also defines the method isAllowed
:
auto filter(const auto& in) { auto ret = decltype(in) {}; for(auto x : in) if(isAllowed(x)) ret.insert(x); return ret; }
This is a clear case where copy_if
could be used instead. I see two alternative versions:
auto filter(const auto& in) { auto ret = decltype(in) {}; copy_if(begin(in), end(in), inserter(ret, end(ret)), [this](auto i) {return this->isAllowed(i);}); return ret; }
or
auto filter(const auto& in) { auto ret = decltype(in) {}; copy_if(begin(in), end(in), inserter(ret, end(ret)), bind(&A::isAllowed, this, placeholders::_1)); return ret; }
Both seem much less obvious than the original, so I am inclined to keep the for
loop. Is there a strong argument not to? (And is there a better way?)
Otherwise, I feel itchy because cases like this point to the limited usefulness of the algorithm tools, despite what best practices advise.
There are probably good arguments for choosing copy_if
that are not being considered properly (performance?). This question is mainly after those, as the others seem more intuitive and easier to get.