1 | #include <cstdio> |
---|---|
2 | #include <string> |
3 | #include <vector> |
4 | |
5 | // If we have libc++ 4.0 or greater we should have <optional> |
6 | // According to libc++ C++1z status page |
7 | // https://libcxx.llvm.org/cxx1z_status.html |
8 | #if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 4000 |
9 | #include <optional> |
10 | #define HAVE_OPTIONAL 1 |
11 | #else |
12 | #define HAVE_OPTIONAL 0 |
13 | #endif |
14 | |
15 | int main() { |
16 | bool has_optional = HAVE_OPTIONAL; |
17 | |
18 | printf(format: "%d\n", has_optional); // break here |
19 | |
20 | #if HAVE_OPTIONAL == 1 |
21 | using int_vect = std::vector<int>; |
22 | using optional_int = std::optional<int>; |
23 | using optional_int_vect = std::optional<int_vect>; |
24 | using optional_string = std::optional<std::string>; |
25 | |
26 | optional_int number_not_engaged; |
27 | optional_int number_engaged = 42; |
28 | |
29 | printf(format: "%d\n", *number_engaged); |
30 | |
31 | optional_int_vect numbers{{1, 2, 3, 4}}; |
32 | |
33 | printf(format: "%d %d\n", numbers.value()[0], numbers.value()[1]); |
34 | |
35 | optional_string ostring = "hello"; |
36 | |
37 | printf(format: "%s\n", ostring->c_str()); |
38 | #endif |
39 | |
40 | return 0; // break here |
41 | } |
42 |