Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2021-03-22 20:23:32


On 3/21/21 11:48 PM, Gavin Lambert via Boost wrote:
> On 22/03/2021 5:48 pm, Robert Ramey wrote:
>> I have a really dumb question about this.  Consider the count_if
>> example.  I understand how example works.  But I can't figure out
>> where the parameter _1 gets filled in. So I think that the count_if
>> example should be completed with a small bit of code which uses the
>> newly count_if function.
>
> The _1 parameter is filled in by std::count_if itself (in this case,
> using each element of the passed iterators).  That's the point of a
> lambda, so I'm not quite sure what you're unclear on.  This is not
> creating a "count_if" function, if that's what you thought; it is a
> complete example including both definition and usage.
>
>
> Having said that, I do think the docs could benefit by having some
> "compare and contrast" examples -- i.e. showing how each of those
> examples would have been implemented using C++11 lambdas or std::bind
> instead (and thus demonstrate the syntactic benefits of the library).
>

OK - I see this now. I knew is something dumb. I haven't needed lambdas
much in the work I've done. I think the following as an example

#include <boost/lambda2.hpp>
#include <algorithm>
#include <vector>
#include <iostream>

void main(){
     std::vector x = {0,1,3,4,234,2,234,55,23};

     std::cout
         << "using standard lambda - number of even values is: "
         << "std::count_if(
             x.begin(),
             x.end,
            bool [](x::value_type z){
                return z % 2 == 0
            }
        );
        << '\n';

     // using "boost::lambda2"
     std::cout
         << "using "boost::lambda2 - number of even values is: "
         << "std::count_if(
             x.begin(),
             x.end,
             boost::_1 % 2 == 0
        );
        << '\n';
}

// explain why lambda2 would be preferred in this case. That is, what
improvement does it provide?

And of course it would provide a perfect place to include a short
description of the motivation for such a library.

Robert Ramey
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk