Boost logo

Boost :

Subject: Re: [boost] [iterator] function_output_iterator constructed from a lambda function is not assignable
From: Paul Fultz II (pfultz2_at_[hidden])
Date: 2016-02-14 21:59:20


On Sunday, February 14, 2016 at 6:28:47 PM UTC-6, Mikhail Matrosov wrote:
>
> Consider the following code snippet:
>
> auto f = [](int x) { std::cout << x; };auto it =
> boost::make_function_output_iterator(f);decltype(it) it2 = it; // Ok,
> copied
> it2 = it; // Does not compile, cannot assign!
>
> The problem is, function_output_iterator constructed in this way is not
> assignable, and thus does not satisfy the Iterator
> <http://en.cppreference.com/w/cpp/concept/Iterator> concept, which
> requires
> type to be CopyAssignable
> <http://en.cppreference.com/w/cpp/concept/CopyAssignable>.
>

One way to deal with this is to put it into boost::optional. However, it now
needs to be dereferenced. This can be easily done with `fit::indirect`:

template<class F>
fit::indirect_adaptor<boost::optional<F>> regular(F f)
{
    return fit::indirect(boost::make_optional(f));
}

See http://fit.readthedocs.org/en/latest/indirect/index.html

Another option is to use `std::ref`, but this requires carefully managing
the
lifetime of the lambda so it lives as long as the iterator. In some
circumstances this is doable.

Paul


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