I am trying to implement the Maybe monad from Haskell using the lambda functions in C++11 and templates. Here\'s what I have so far
#include
#i
The following works for me: I use decltype to infer the type returned by the lambda:
template
auto operator>>=(Maybe t, Func f) -> decltype(f(t.data))
{
decltype(f(t.data)) return_value;
if(t.valid == false)
{
return_value.valid = false;
return return_value;
}
else
{
return f(t.data);
}
}
EDIT
For type safety :
template
struct Maybe
{
T1 data;
bool valid;
static const bool isMaybe = true;
};
template
auto operator>>=(Maybe t, Func f) -> decltype(f(t.data))
{
typedef decltype(f(t.data)) RT;
static_assert(RT::isMaybe, "F doesn't return a maybe");
...