-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
51 lines (38 loc) · 1.5 KB
/
Copy pathmain.cpp
File metadata and controls
51 lines (38 loc) · 1.5 KB
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
#include "optional.hpp"
#include <iostream>
#include <limits>
#include <string>
inline void hello(optional<std::string> what)
{
what.if_present([](const std::string& x) { std::cout << "Hello, " << x << std::endl; });
}
inline double convert(optional<std::string> what)
{
return what.map(&std::stod, static_cast<size_t*>(0)).or_else(std::numeric_limits<double>::quiet_NaN());
}
int main()
{
hello({});
hello("World!");
std::cout << convert("5.36") << std::endl;
std::cout << convert({}) << std::endl;
std::cout << "c str: " << optional<std::string>("hi").map(&std::string::c_str).or_else(nullptr) << std::endl;
optional<int> l;
std::cout << "l or 55: " << l.or_else(55) << std::endl;
std::cout << "l or 3*3: " << l.or_else_get([] { return 3 * 3; }) << std::endl;
// hash of empty optional
std::cout << "hash of empty optional==0: " << (std::hash<optional<int>>()(l) == std::hash<int>()(0)) << std::endl;
l.if_present_or_else([](int x) { std::cout << "l is present: " << x << std::endl; },
[&l] {
std::cout << "l is not present; setting it to 61" << std::endl;
l.emplace(61);
});
std::cout << "l as double + 10%: " << l.map([](int x) { return x * 1.1; }).get() << std::endl;
try {
optional<int> x;
// getting empty optional
x.get();
} catch (const no_such_element_error& e) {
std::cout << e.what() << std::endl;
}
}