Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2021-03-23 18:07:18


Fletcher, John P wrote:
> Hi
>
> I did some tests with lambda2 some time ago and the review release seems to
> be the same. One of the useful things in the old lambda is this example:
>
> for_each(a.begin(), a.end(), std::cout << _1 << ' ');
>
> I could not get this to work with lambda2. I am not in a position to run my
> tests again at the moment. It would be good if this example did work.

Despite being deceptively simple, this is actually a pretty thorny example for
lambda libraries.

First off, Lambda2 doesn't even support operator<<, because there's no such
function object in <functional>, but let's suppose we fix that.

Then, there are two additional obstacles. First, std::cout needs to be taken by
reference, but lambda libraries traditionally default to capture by value, and
by-ref captures need to be marked with std::ref (or boost::ref). Boost.Lambda
gets around that by hardcoding std::ostream& to be taken by reference by
default.

Second, std::endl is a function template, so template argument deduction of
the second operator<< fails against it. Boost.Lambda fails on this
one: https://godbolt.org/z/Ec4MYhG56

Boost.Phoenix works as-is, probably by special-casing the stream
manipulators: https://godbolt.org/z/5z6vc3bT9

It'd be easy to make the version

    std::ref(std::cout) << _1 << '\n'

work with Lambda2, but the example as-is requires more substantial changes.


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