Back
Close

Modern C++ idoms and recipes

meshell
58K views

std::variant

In the previous exercise we used std::optional to indicate failure. But how about a more detailed error message?

Enter template class std::variant. We could use std::variant to either hold a valid value or a detailed error.

DIY

Refactor function differentiate to have return type std::variant<double, std::string> and once again make the tests pass. The variant should either be a valid value or an error string.

Refactor the code to use std::variant
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io