Boost logo

Boost :

Subject: Re: [boost] [Optional] Monadic bind
From: Tobias Loew (Tobias.Loew_at_[hidden])
Date: 2015-06-23 03:22:11


Von: Andrzej Krzemienski [via Boost] [mailto:ml-node+s2283326n4677451h87_at_[hidden]]
Gesendet: Montag, 22. Juni 2015 20:54
An: Löw, Tobias (STEAG Energy Services GmbH)
Betreff: Re: [Optional] Monadic bind

2015-06-22 16:59 GMT+02:00 Tobias Loew <[hidden email]</user/SendEmail.jtp?type=node&node=4677451&i=0>>:

> I deliberately used this std::bind syntax since it accepts any callable
> (including things like pointer-to-member [functions]).
> I tried lambdas with my code and they just work. E.g.
>
> auto opt_i3 = map(opt_a, [](const A& a){ return a.i;});
> assert(*opt_i3 == 42);
>

Interesting. Is it implementable in C++03?

I managed to change your map2 to compile in C++03 (on VS 2008)

    template <typename T, typename F>
    boost::optional<
        typename boost::remove_cv<
            typename boost::remove_reference<
                typename boost::result_of<F(T)>::type
>::type
>::type
>
    map2(const boost::optional<T>& o, F f)
    {
        if (o) return f(*o);
        else return boost::none;
    }

I didn’t find an easy way to make my map compile in C++03. Technically, it should be possible by using the same tricks that boost::bind itself uses, but I think that would be by far too much for such a feature.
A good solution could be: using a map2-like version (only accepting functions/functors) for C++03 compilers and a the more general version (accepting arbitrary callables) for C++11 compilers.

--
View this message in context: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677464.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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