Skip to content

Instantly share code, notes, and snippets.

@bramblex
Created September 25, 2017 05:31
Show Gist options
  • Save bramblex/9321aa5067437c4516d6e17a110f28ae to your computer and use it in GitHub Desktop.
Save bramblex/9321aa5067437c4516d6e17a110f28ae to your computer and use it in GitHub Desktop.
template <class T, class E>
union ResultContent {
T data;
E error;
};
template<class T = bool, class E = bool>
struct Result {
protected:
bool ok;
ResultContent<T, E> content;
public:
Result(bool ok, ResultContent<T, E> content) {
this->ok = ok;
this->content = content;
}
static Result<T, E> Succ(){
ResultContent<T, E> content;
content.data = true;
return Result<T, E>(true, content);
}
static Result<T, E> Fail(){
ResultContent<T, E> content;
content.data = false;
return Result<T, E>(false, content);
}
static Result<T, E> Succ(T data){
ResultContent<T, E> content;
content.data = data;
return Result<T, E>(true, content);
}
static Result<T, E> Fail(E err){
ResultContent<T, E> content;
content.err = err;
return Result<T, E>(false, content);
}
T getData() {
return this->content.data;
}
E getErr() {
return this->content.err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment