Created
September 25, 2017 05:31
-
-
Save bramblex/9321aa5067437c4516d6e17a110f28ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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