When using std::variant
, there are a few key ways to handle errors and exceptional situations:
std::bad_variant_access
This exception is thrown if you call std::get
 with an index or type that doesn't match the currently active variant member. To avoid this, you can:Here's an example of using std::get_if
:
std::holds_alternative
 to check if the variant holds a specific type before trying to access it.std::get_if
, which returns a pointer to the held value if it's of the specified type, or nullptr
 otherwise.std::visit
 with a visitor that handles all possible types.std::variant<int, std::string> v = 42;
if (auto pint = std::get_if<int>(&v)) {
std::cout << "int: " << *pint << '\n';
} else if (auto pstr = std::get_if<std::string>(&v)) {
std::cout << "string: " << *pstr << '\n';
}
A variant can become "valueless by exception" if an exception is thrown during assignment or emplacement. You can check for this state with the valueless_by_exception()
 member function. A variant in this state can still be assigned to, but any other operation will throw std::bad_variant_access
.
try {
v.emplace<std::string>(invalid_string);
} catch (...) {
if (v.valueless_by_exception()) {
std::cout << "variant became valueless\n";
}
}
If an exception is thrown from a visitor function during a call to std::visit()
, the exception will propagate out of the call to visit
. Ensure your visitors handle exceptions appropriately.
try {
std::visit(my_visitor, v);
} catch (const my_exception& e) {
std::cerr << "Caught exception from visitor: "
<< e.what() << '\n';
}
Some general best practices:
std::variant
 only when the set of types is known at compile-time and is relatively small.std::visit
 over directly accessing variant members. It's more type-safe and cleaner.std::bad_variant_access
 exceptions, either by preventing them with checks like std::holds_alternative
, or catching them if they're expected.By following these practices, you can use std::variant
safely and effectively in your C++Â code.
Answers to questions are automatically generated and may not have been reviewed.
std::variant
Learn how to store dynamic data types in C++ using unions and the type-safe std::variant