[off-topic] why do we not have std::copy_if?

I've recently wondered why std::copy doesn't have an analogous std::copy_if: template<class InIt, class OutIt, class Pr> OutIt *copy_if*(InIt first, InIt last, OutIt dest, Pr pred); that would copy elements to dest where the predicate returns true. Not unlike "grep" in Perl and "filter" in Python. The same goal can be achieved with std::remove_copy_if and the use of a sense-reversed predicate, but this leads to confusing code IMHO. Why wouldn't this be part of the Standard Library, or am I missing something ridiculously obvious? -- Caleb Epstein caleb dot epstein at gmail dot com

Caleb Epstein wrote:
I've recently wondered why std::copy doesn't have an analogous std::copy_if:
template<class InIt, class OutIt, class Pr> OutIt *copy_if*(InIt first, InIt last, OutIt dest, Pr pred);
that would copy elements to dest where the predicate returns true. Not unlike "grep" in Perl and "filter" in Python.
The same goal can be achieved with std::remove_copy_if and the use of a sense-reversed predicate, but this leads to confusing code IMHO.
Why wouldn't this be part of the Standard Library, or am I missing something ridiculously obvious?
I guess it should have been part of C++98, but didn't make it due to perhaps time contraints. With the advent of ranges, we might make *all* the xxx_if versions of algorithms redundant by replacing xxx_if( range, pred ) with xxx( range | filtered( pred ) ); -Thorsten

What you mean by the ranges? Although I'm learning it fast, I'm still fairly new to the STL and Boost. I don't think I've seen this yet. -- Bill --
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Thorsten Ottosen Sent: Tuesday, March 14, 2006 7:08 AM To: boost@lists.boost.org Subject: Re: [boost] [off-topic] why do we not have std::copy_if?
Caleb Epstein wrote:
I've recently wondered why std::copy doesn't have an analogous std::copy_if:
template<class InIt, class OutIt, class Pr> OutIt *copy_if*(InIt first, InIt last, OutIt dest, Pr pred);
that would copy elements to dest where the predicate returns true. Not unlike "grep" in Perl and "filter" in Python.
The same goal can be achieved with std::remove_copy_if and the use of a sense-reversed predicate, but this leads to confusing code IMHO.
Why wouldn't this be part of the Standard Library, or am I missing something ridiculously obvious?
I guess it should have been part of C++98, but didn't make it due to perhaps time contraints.
With the advent of ranges, we might make *all* the xxx_if versions of algorithms redundant by replacing xxx_if( range, pred ) with
xxx( range | filtered( pred ) );
-Thorsten _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Bill Buklis wrote:
What you mean by the ranges? Although I'm learning it fast, I'm still fairly new to the STL and Boost. I don't think I've seen this yet.
See http://www.boost.org/libs/range/index.html or perhaps http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1871.html -Thorsten

Caleb Epstein wrote:
I've recently wondered why std::copy doesn't have an analogous std::copy_if:
template<class InIt, class OutIt, class Pr> OutIt *copy_if*(InIt first, InIt last, OutIt dest, Pr pred);
that would copy elements to dest where the predicate returns true. Not unlike "grep" in Perl and "filter" in Python.
The same goal can be achieved with std::remove_copy_if and the use of a sense-reversed predicate, but this leads to confusing code IMHO.
Why wouldn't this be part of the Standard Library, or am I missing something ridiculously obvious?
IIRC, in previous discussions of this topic, the consensus was that it was an oversight. Josuttis mentions the lack of this algorithm, directing readers to use remove_copy_if. Jeff Flinn

"Caleb Epstein" <caleb.epstein@gmail.com> writes:
I've recently wondered why std::copy doesn't have an analogous std::copy_if:
template<class InIt, class OutIt, class Pr> OutIt *copy_if*(InIt first, InIt last, OutIt dest, Pr pred);
that would copy elements to dest where the predicate returns true. Not unlike "grep" in Perl and "filter" in Python.
The same goal can be achieved with std::remove_copy_if and the use of a sense-reversed predicate, but this leads to confusing code IMHO.
Why wouldn't this be part of the Standard Library, or am I missing something ridiculously obvious?
This is the wrong forum for this question. Please take it to an appropriate mailing list or newsgroup, such as c.l.c++.m I don't see why anyone would knowingly start a thread with [off-topic] in the subject line. http://boost.org/more/discussion_policy.htm: Off-topic posts Discussions which stray from the acceptable topics are strongly discouraged. While off-topic posts are often well meaning and not as individually corrosive as other abuses, cumulatively the distraction damages the effectiveness of discussion. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (5)
-
Bill Buklis
-
Caleb Epstein
-
David Abrahams
-
Jeff Flinn
-
Thorsten Ottosen