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
> <> concept, which
> requires
> type to be 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));


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


Boost list run by bdawes at, gregod at, cpdaniel at, john at