From 96ef5fb76938f5d3ce41a6306e508d7752a5b2d2 Mon Sep 17 00:00:00 2001 From: franneck94 Date: Fri, 3 Nov 2023 16:19:44 +0100 Subject: [PATCH] up --- 10_STL2/expected.cc | 40 +++++++++++++++++++++------------------- TODO.md | 1 - 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/10_STL2/expected.cc b/10_STL2/expected.cc index 5069f3a..08a947d 100644 --- a/10_STL2/expected.cc +++ b/10_STL2/expected.cc @@ -1,23 +1,15 @@ #include #include -#include #include using namespace std::string_literals; -std::optional check(const bool flag) -{ - if (flag) - return "true"s; - return {}; // std::nullopt -} - enum class parse_error { - invalid_input, + invalid_input }; -std::expected check2(const bool flag) +std::expected check(const bool flag) { if (flag) return "true"s; @@ -26,15 +18,25 @@ std::expected check2(const bool flag) int main() { - auto v1 = std::optional{42}; - std::cout << *v1 << '\n'; // 42 - - auto v2 = std::optional{"text"s}; - std::cout << v2.value() << '\n'; // text - auto v3 = std::optional{}; - std::cout << v3.value_or("default"s) << '\n'; // default - - std::cout << std::boolalpha << v3.has_value() << '\n'; + auto result1 = check(false); + if (result1.has_value()) + { + std::cout << "value: " << result1 << "\n"; + } + else if (result1.error() == parse_error::invalid_input) + { + std::cout << "error!\n"; + } + + auto result2 = check(false); + if (result2.has_value()) + { + std::cout << "value: " << result1 << "\n"; + } + else if (result2.error() == parse_error::invalid_input) + { + std::cout << "error!\n"; + } return 0; } diff --git a/TODO.md b/TODO.md index 0a7a571..0c583c9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ # TODO - Lektion 125: neu aufnehmen -- std::expected Video aufnehmen, soll in kapitel 10 sein