Result - Simple monad solution based on C++17 and policy based design
#include"result/result.hpp"
#include<iostream>using Result = Result<int, std::string>;
constexprint forty_and_two{42};
static std::string const error_string{"Bad wolf"};
Result answer(int answer)
{
return forty_and_two == answer ? Result{answer} : Result{error_string};
}
intmain()
{
auto ok_result = answer(forty_and_two);
if(ok_result)
{
std::cout << ok_result.value() << '\n';
}
return0;
}
{}