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-19 13:12:27


On Monday, February 15, 2016 at 2:36:02 PM UTC-6, Andrey Semashev wrote:
>
> On 2016-02-14 22:42, Mikhail Matrosov wrote:
> > Consider the following code snippet:
> >
> > auto f = ;auto it =
> > boost::make_function_output_iterator(f);decltype(it) it2 = it; // Ok,
> > copied
> > it2 = it; // Does not compile, cannot assign!
>
> Since the lambda does not capture anything, you might try to convert it
> to a function pointer, which would make it assignable.
>
> typedef void (*lambda_t)(int);
> auto it = make_function_output_iterator((lambda_t)f);
>
> If you do have to capture variables, you might want to use std::bind to
> store the captures and a pointer to the lambda function without
> captures. Or just use std::bind with a normal function.
>

That is another way. Its simpler just to append a `+` to do that though:

    auto it = make_function_output_iterator(+[](int x) { std::cout << x; });

Paul


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