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);
Furthermore without the std::decay boost::optional will be a boost::optional.
Tobias
Von: Andrzej Krzemienski [via Boost] [mailto:ml-node+s2283326n4677436h22@n4.nabble.com]
Gesendet: Montag, 22. Juni 2015 16:44
An: Löw, Tobias (STEAG Energy Services GmbH)
Betreff: Re: [Optional] Monadic bind
2015-06-22 16:17 GMT+02:00 Tobias Loew <[hidden email]>:
Sorry for that,
I've made in implementation with free functions which compiles on VS 2012:
#include
#include
template
auto bind(const boost::optional<T>& optional, B&& binder)
-> typename std::decay::type
{
if(optional)
{
return std::bind(binder, *optional)();
}
else
{
return boost::none;
}
}
template
auto map(const boost::optional<T>& optional, B&& binder)
-> boost::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
#include
#include
template
auto map2(const boost::optional<T>& o, F f)
-> boost::optional
{
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
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
________________________________
If you reply to this email, your message will be added to the discussion below:
http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677436.html
To unsubscribe from [Optional] Monadic bind, click here.
NAML
--
View this message in context: http://boost.2283326.n4.nabble.com/Optional-Monadic-bind-tp4677431p4677437.html
Sent from the Boost - Dev mailing list archive at Nabble.com.