Exploring optional return values.
This commit is contained in:
parent
c736387063
commit
67cbd430bf
1 changed files with 23 additions and 0 deletions
|
@ -1,8 +1,31 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
optional<string> create(bool b)
|
||||||
|
{
|
||||||
|
if(b) {
|
||||||
|
return "Godzilla";
|
||||||
|
} else {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto create2(bool b)
|
||||||
|
{
|
||||||
|
return b ? optional<string>{"Godzilla"} : nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue