Skip to content

Instantly share code, notes, and snippets.

@tforgione
Last active September 6, 2017 09:55
Show Gist options
  • Save tforgione/8d1670f2b72176564996f8d5777ec39c to your computer and use it in GitHub Desktop.
Save tforgione/8d1670f2b72176564996f8d5777ec39c to your computer and use it in GitHub Desktop.
#ifndef RESULT_HPP
#define RESULT_HPP
namespace ut {
template<typename T, typename E>
union result_data {
T data_ok;
E data_err;
};
template<typename T, typename E>
struct result {
public:
result(T const& t) : _ok(true), _data {t} {};
result(E const& e) : _ok(false), _data {.data_err={e}} {};
T const& ok() const { return _data.data_ok; }
E const& err() const { return _data.data_err; }
T& ok() { return _data.data_ok; }
E& err() { return _data.data_err; }
bool is_ok() const { return _ok; };
bool is_err() const { return ! _ok; };
private:
bool _ok;
result_data<T, E> _data;
};
template<typename T, typename E>
result<T, E> Ok(T const& t)
{
return result<T, E>{t};
};
template<typename T, typename E>
result<T, E> Err(E const& e)
{
return result<T, E>{e};
};
}
#endif // RESULT_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment