Skip to content

Instantly share code, notes, and snippets.

@wolkym
Created September 30, 2016 15:04
Show Gist options
  • Save wolkym/93b6431b91d7d4dc882e49006e12f133 to your computer and use it in GitHub Desktop.
Save wolkym/93b6431b91d7d4dc882e49006e12f133 to your computer and use it in GitHub Desktop.
C++ optional
template<typename T>
class Option{
private:
T* some;
bool is_null;
public:
Option(T* obj_) : some(obj_), is_null(obj_ == nullptr) { }
bool isEmpty(){ return is_null;}
bool isDefined(){ return !isEmpty;}
template<typename Func, typename U>
Option<U> Map(Func f)
{
if(isDefined())
{
return Option<U>(f(some));
} else {
return Option<U>(nullptr);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment