summaryrefslogtreecommitdiff
path: root/test.cpp
blob: 534dfca927f10dff7f49a23443ce1982e4560b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}