#pragma once #include template class Optional { public: static Optional of(T value) { Optional result; result.exists = true; result.value = value; return result; } static Optional _empty() { Optional result; result.exists = false; return result; } /** * Check whether value exists or not **/ bool absent() const { return !exists; } T orValue(T value) const { return exists ? this->value : value; } /** * return the element, or throw if absent * @throws std::logic_error * @return T value **/ T get() const { if (!exists) throw std::logic_error("Optional does not contains value"); return value; } T operator*() const { return get(); } static Optional empty; protected: T value; bool exists; private: Optional() {} }; template Optional Optional::empty = Optional::_empty();