Skip to content

Instantly share code, notes, and snippets.

@orbekk
Forked from ehamberg/maybe.cpp
Last active December 26, 2015 08:19
Show Gist options
  • Save orbekk/7121746 to your computer and use it in GitHub Desktop.
Save orbekk/7121746 to your computer and use it in GitHub Desktop.
template <class T>
struct Maybe {
public:
Maybe() {}
explicit Maybe(T v) : state_(v) {}
bool isJust() {
return state_;
}
T get() {
assert state_;
return *state_;
}
T fromMaybe(const T& d) {
if (state_) {
return *state_;
} else {
return d;
}
}
private:
unique_ptr<T> state_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment