Boost logo

Boost Users :

Subject: Re: [Boost-users] Oven, regular and Boost
From: Akira Takahashi (faithandbrave_at_[hidden])
Date: 2012-12-10 07:22:58


2012/12/10 Robert Jones <robertgbjones_at_[hidden]>

> The 'Oven' port...
>
>
> http://dl.dropbox.com/u/1682460/git/OvenToBoost/libs/range/doc/html/range_extension/design_rationale/regular_op.html
>
> makes much of its 'regular' operator, which is used to fix this problem
>
> using boost::lambda::_1;
> for_each_(r | filtered(_1 % 2 == 0), f); // Error! Can't default
> construct/copy assign
> for_each_(r | filtered(regular(_1 % 2 == 0)), f); // OK
>
> However, AFAICS this is not a problem in current-ishBoost (1.47). Has this
> ever been a problem? Can anyone
> comment on its general provenance?
>
> Thx, Rob.
>
>
You can try this code.
for_each_() is possible std::for_each()'s implementation.

#include <iostream>
#include <vector>
#include <boost/lambda/lambda.hpp>
#include <boost/range/adaptor/filtered.hpp>

void disp(int x)
{
    std::cout << x << std::endl;
}

template <class InputIterator, class F>
void for_each_(InputIterator first, InputIterator last, F f)
{
    InputIterator it; // Default Constructible
    it = first; // Copy Assignable
    for (; it != last; ++it) {
        f(*it);
    }
}

template <class SinglePassRange, class F>
void for_each_(const SinglePassRange& rng, F f)
{
    return for_each_(boost::begin(rng), boost::end(rng), f);
}

int main()
{
    using boost::lambda::_1;
    using namespace boost::adaptors;

    const std::vector<int> v = {1, 2, 3, 4, 5};
    for_each_(v | filtered(_1 % 2 == 0), disp);
}

Boost.Lambda and C++11 lambda expression has this issue yet.
And this issue is not a bug. I think this is lambda's specification.

-- 
>>========================
Akira Takahashi


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