Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
franneck94 committed Nov 3, 2023
1 parent 0cd2b73 commit 96ef5fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
40 changes: 21 additions & 19 deletions 10_STL2/expected.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
#include <expected>
#include <iostream>
#include <optional>
#include <string>

using namespace std::string_literals;

std::optional<std::string> check(const bool flag)
{
if (flag)
return "true"s;
return {}; // std::nullopt
}

enum class parse_error
{
invalid_input,
invalid_input
};

std::expected<std::string, parse_error> check2(const bool flag)
std::expected<std::string, parse_error> check(const bool flag)
{
if (flag)
return "true"s;
Expand All @@ -26,15 +18,25 @@ std::expected<std::string, parse_error> check2(const bool flag)

int main()
{
auto v1 = std::optional<int>{42};
std::cout << *v1 << '\n'; // 42

auto v2 = std::optional<std::string>{"text"s};
std::cout << v2.value() << '\n'; // text
auto v3 = std::optional<std::string>{};
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;
}
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO

- Lektion 125: neu aufnehmen
- std::expected Video aufnehmen, soll in kapitel 10 sein

0 comments on commit 96ef5fb

Please sign in to comment.