...

/

Returning std::optional

Returning std::optional

Let's learn the different ways to return an optional.

We'll cover the following...

If you return an optional from a function, then it’s very convenient to return just std::nullopt or the computed value.

Press + to interact
std::optional<std::string> TryParse(Input input) {
if (input.valid())
return input.asString();
return std::nullopt;
}
// use:
auto oStr = TryParse(Input{...});

In the above example you can see that the function returns std::string computed ...