在线文档教程
C++
应用 | Utilities

std::bad_variant_access

STD:不好[医]变体[医]存取

Defined in header
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) virtualdestructs the exception object (virtual public member function of std::exception)
what virtualreturns an explanatory string (virtual public member function of std::exception)

二次

#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'; } }

二次

可能的产出:

二次

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)

© cppreference.com

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

http://en.cppreference.com/w/cpp/实用程序/变体/坏[医]变体[医]存取