| 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 | struct X { |
| 16 | int x; |
| 17 | }; |
| 18 | |
| 19 | int main() { |
| 20 | bool has_optional = HAVE_OPTIONAL; |
| 21 | |
| 22 | printf(format: "%d\n", has_optional); // break here |
| 23 | |
| 24 | #if HAVE_OPTIONAL == 1 |
| 25 | using int_vect = std::vector<int>; |
| 26 | using optional_int = std::optional<int>; |
| 27 | using optional_int_vect = std::optional<int_vect>; |
| 28 | using optional_string = std::optional<std::string>; |
| 29 | |
| 30 | optional_int number_not_engaged; |
| 31 | optional_int number_engaged = 42; |
| 32 | std::optional<X> x = X{.x: 42}; |
| 33 | |
| 34 | printf(format: "%d\n", *number_engaged); |
| 35 | |
| 36 | optional_int_vect numbers{{1, 2, 3, 4}}; |
| 37 | |
| 38 | printf(format: "%d %d\n", numbers.value()[0], numbers.value()[1]); |
| 39 | |
| 40 | optional_string ostring = "hello"; |
| 41 | |
| 42 | printf(format: "%s\n", ostring->c_str()); |
| 43 | #endif |
| 44 | |
| 45 | return 0; // break here |
| 46 | } |
| 47 |
