From 67cbd430bfcdae585b7fa36a116d489b071b36e3 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 4 May 2024 21:06:10 -0400 Subject: [PATCH] Exploring optional return values. --- PPP3/ex08.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/PPP3/ex08.cpp b/PPP3/ex08.cpp index e467e6d..1dc5827 100644 --- a/PPP3/ex08.cpp +++ b/PPP3/ex08.cpp @@ -1,8 +1,31 @@ #include +#include +#include using namespace std; +optional create(bool b) +{ + if(b) { + return "Godzilla"; + } else { + return {}; + } +} + +auto create2(bool b) +{ + return b ? optional{"Godzilla"} : nullopt; +} + int main() { + cout << "create(false) returned " + << create(false).value_or("empty") << "\n"; + + if(auto str = create2(true)) { + cout << "create2(true) returned " << *str << " with size " << str->size() << "\n"; + } + return 0; }