-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptional.cpp
49 lines (38 loc) · 1.07 KB
/
Optional.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* \file main.cpp
* \brief std::optional
*
* \todo
*
* The class template std::optional manages an optional contained value, i.e. a value that may or
* may not be present. A common use case for optional is the return value of a function
* that may fail.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
std::optional<std::string>
create(bool b)
{
if (b) {
return "Godzilla";
} else {
return {};
}
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
create(false).value_or("empty"); // == "empty"
create(true).value(); // == "Godzilla"
// optional-returning factory functions are usable as conditions of while and if
if (auto str = create(true)) {
// ...
}
// std::cout << STD_TRACE_VAR("") << std::endl;
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
#endif