diff options
Diffstat (limited to 'test.cpp')
-rw-r--r-- | test.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..534dfca --- /dev/null +++ b/test.cpp @@ -0,0 +1,55 @@ +#include <iostream> + +using std::cout; + +template<typename T> +class Option { +public: + Option<T>() { + exists = false; + } + Option<T>(T const &t) { + set(t); + } + void set(T const &t) { + exists = true; + x = t; + } + void clear() { + exists = false; + } + T *get() { + if (exists) + return &x; + else + return nullptr; + } + T const *get() const { + if (exists) + return &x; + else + return nullptr; + } +private: + bool exists; + T x; +}; + +template<typename T> +void print_option(Option<T> const &o) { + T const *ptr = o.get(); + if (ptr) + cout << *ptr << "\n"; + else + cout << "None\n"; +} + +int main() { + Option<int> o(7); + print_option(o); + o.clear(); + print_option(o); + o.set(133); + print_option(o); + return 0; +} |