| 1 |
#include <iostream> |
| 2 |
#include <sstream> |
| 3 |
#include <string> |
| 4 |
#include <optional> |
| 5 |
|
| 6 |
using namespace std; |
| 7 |
|
| 8 |
optional<string> nextLine(istream &in) |
| 9 |
{ |
| 10 |
std::optional<std::string> opt; |
| 11 |
|
| 12 |
string line; |
| 13 |
if (getline(in, line)) |
| 14 |
opt = move(line); |
| 15 |
|
| 16 |
cout << "internal: has value: " << opt.has_value() << |
| 17 |
", value = " << *opt << '\n'; |
| 18 |
return opt; |
| 19 |
} |
| 20 |
|
| 21 |
int main() |
| 22 |
{ |
| 23 |
istringstream in{ "hello world\n" }; |
| 24 |
|
| 25 |
auto opt = nextLine(in); |
| 26 |
|
| 27 |
cout << "main: has value: " << opt.has_value() << |
| 28 |
", value = " << *opt << '\n'; |
| 29 |
opt = nextLine(in); |
| 30 |
|
| 31 |
cout << "main: has value: " << opt.has_value() << |
| 32 |
", value = " << *opt << '\n'; |
| 33 |
} |