With std::variant

The lesson describes how std::variant is better than std::optional in terms of error handling.

How to do Error Handling using std::variant?

The last implementation with std::optional omits one crucial aspect: error handling.

There’s no way to know the reason why a value wasn’t computed. For example with the version where std::pair was used, we were able to return an error code to indicate the reason. What can we do about that?

If you need full information about the error that might occur in the function, you can think about an alternative approach with std::variant.

Get hands-on with 1200+ tech skills courses.