$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] [Optional] Monadic bind
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2015-06-22 11:04:24
2015-06-22 16:17 GMT+02:00 Tobias Loew <Tobias.Loew_at_[hidden]>:
> Sorry for that,
>
> I've made in implementation with free functions which compiles on VS 2012:
>
>
> #include <assert.h>
> #include <boost/optional.hpp>
>
>
> template<class T, class B>
> auto bind(const boost::optional<T>& optional, B&& binder)
>     -> typename std::decay<decltype(std::bind(binder, *optional)())>::type
> {
>     if(optional)
>     {
>         return std::bind(binder, *optional)();
>     }
>     else
>     {
>         return boost::none;
>     }
> }
>
> template<class T, class B>
> auto map(const boost::optional<T>& optional, B&& binder)
>     -> boost::optional<typename std::decay<decltype(std::bind(binder,
> *optional)())>::type>
> {
>     if(optional)
>     {
>         return std::bind(binder, *optional)();
>     }
>     else
>     {
>         return boost::none;
>     }
> }
>
>
> void foo()
> {
>     struct A {
>         int i;
>     };
>     boost::optional<A> opt_a;
>
>     auto opt_i = map(opt_a, &A::i);
>     assert(!opt_i);
>
>     A a = {42};
>     opt_a = a;
>
>     auto opt_i2 = map(opt_a, &A::i);
>     assert(*opt_i2 == 42);
>
> }
>
Would you find the following an acceptable solution?
```
#include <assert.h>
#include <boost/bind.hpp>
#include <boost/optional.hpp>
template <typename T, typename F>
auto map2(const boost::optional<T>& o, F f)
  -> boost::optional<decltype(f(*o))>
{
  if (o) return f(*o);
  else   return boost::none;
}
void foo()
{
    struct A {  int i; };
    boost::optional<A> opt_a;
    auto opt_i = map2(opt_a, boost::bind(&A::i, _1));
    assert(!opt_i);
    A a = {42};
    opt_a = a;
    auto opt_i2 = map2(opt_a, boost::bind(&A::i, _1));
    assert(opt_i2);
    assert(*opt_i2 == 42);
}
int main()
{
    foo();
}
```
It requires of you to type a bit more, but at the same time it is more
flexible: you can type any free function on T, including a lambda.
Regards,
&rzej