Boost logo

Boost Users :

Subject: Re: [Boost-users] [range] cannot use lambda predicate in adaptor with certain algorithms
From: Akira Takahashi (faithandbrave_at_[hidden])
Date: 2011-05-30 04:59:08


Hi, Nathan.

This problem is known issue.
PStade.Oven Library have solution by regular function.
http://p-stade.sourceforge.net/oven/doc/html/oven/utilities.html#oven.utilities.regular

I’ve been doing for now, Oven to Boost.Range porting project.
https://github.com/faithandbrave/OvenToBoost

Using this library you can write as follow:

#include <boost/range/algorithm/min_element.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/regular.hpp>
int main()
{
   int a[] = {1, 2, 3};
   auto is_odd = [](int i){ return i % 2 == 1; };
   boost::min_element(a | boost::adaptors::filtered(boost::regular(is_odd)));
}

or, use regular operator(operator|+) version:

#include <boost/range/algorithm/min_element.hpp>
#include <boost/range/adaptor/regular_extension/filtered.hpp>
int main()
{
   int a[] = {1, 2, 3};
   auto is_odd = [](int i){ return i % 2 == 1; };
   boost::min_element(a |+ boost::adaptors::filtered(is_odd));
}

2011/5/30 Nathan Ridge <zeratul976_at_[hidden]>:
>
> Hello,
>
> I get errors related to lambdas not being default-constructible or copy-assignable
> when using a lambda as a predicate with certain adaptors (e.g. filtered) and
> certain algorithms (e.g. min_element).
>
> The following code illustrates the problem:
>
> #include <boost/range/algorithm/min_element.hpp>
> #include <boost/range/adaptor/filtered.hpp>
> int main()
> {
>    int a[] = {1, 2, 3};
>    auto is_odd = [](int i){ return i % 2 == 1; };
>    boost::min_element(a | boost::adaptors::filtered(is_odd));
> }
>
> The errors are shown below.
>
> Is there a workaround for this?
>
> Thanks,
> Nate.
>

>>========================
Akira Takahashi
mailto : faithandbrave_at_[hidden]
blog : http://d.hatena.ne.jp/faith_and_brave/


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net