This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/81482] by-value lambda capture in remove_if


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81482

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |minor

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It's not specific to lambdas, but any stateful predicate.

As you say, this behaviour is conforming. C++2014 [algorithms.general] para 10
says:

[Note: Unless otherwise specified, algorithms that take function objects as
arguments are permitted to copy those function objects freely. Programmers for
whom object identity is important should consider using a wrapper class that
points to a noncopied implementation object such as reference_wrapper<T>
(23.14.5), or some equivalent solution. — end note]


It looks like (at least) std::search and std::search_n and std::find_end make
copies of the predicate and then reuse the original object.

If there's a measurable performance cost to the copies I'd entertain patches to
do something about it (and even then, using reference_wrapper is probably
appropriate), but I'm not concerned by the surprising result for your testcase.
That's how the standard algorithms work. If you capture by reference, or pass
the predicate by reference, then everything works as expected.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]