首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::bad_variant_access

Defined in header <variant>

class bad_variant_access : public std::exception

(since C++17)

std::bad_variant_access在下列情况下引发的异常的类型:

  • std::get(std::variant)使用与当前活动选项不匹配的索引或类型调用。
  • std::visit调用访问一个变体valueless_by_exception

成员函数

(constructor)

bad_variant_access() (public member function)

STD:不好[医]变体[医]准入::糟糕[医]变体[医]接入%28%29

bad_variant_access();

构造一个新的实例。std::bad_variant_access...

参数

%280%29

例外

noexcept规格:

noexcept

继承自STD:例外

成员函数

(destructor) virtual

destructs the exception object (virtual public member function of std::exception)

what virtual

returns an explanatory string (virtual public member function of std::exception)

二次

代码语言:javascript
复制
#include <variant>
#include <iostream>
 
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';
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
bad_variant_access

二次

另见

std::get(std::variant) (C++17)

reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)

visit (C++17)

calls the provided functor with the arguments held by one or more variants (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券