std::bad_variant_access

Header: <variant>

std::bad_variant_access is the type of the exception thrown in the following situations:

# Declarations

class bad_variant_access : public std::exception

(since C++17)

# Parameters

# Return value

*this

# Notes

Implementations are allowed but not required to override what().

# Example

#include <iostream>
#include <variant>
 
int main()
{
    std::variant<int, float> v;
    v = 12;
    try
    {
        std::get<float>(v);
    }
    catch (const std::bad_variant_access& e)
    {
        std::cout << e.what() << '\n';
    }
}

# See also