std::variant<Types...>::valueless_by_exception
来自 cppreference.cn
constexpr bool valueless_by_exception() const noexcept; |
(C++17 起) | |
当且仅当变体持有一个值时返回 false。
[编辑] 注意
在以下情况下,当初始化所包含的值时,变体可能变得无值:
由于变体从不允许分配动态内存,因此在这些情况下,不能保留先前的旧值,因此也无法恢复。如果类型提供不抛出异常的移动操作,并且实现首先在栈上构造新值,然后将其移动到变体中,则“可选”情况可以避免抛出异常。
这甚至适用于非类类型的变体。
struct S { operator int() { throw 42; } }; std::variant<float, int> v{12.f}; // OK v.emplace<1>(S()); // v may be valueless
一个因异常而无值的变体——即由于上述情况之一之前的异常而没有值的变体——被视为处于无效状态
-
index
返回variant_npos
-
get
抛出bad_variant_access
-
visit
和 成员-visit
(C++26 起) 抛出bad_variant_access
[编辑] 示例
运行此代码
#include <cassert> #include <iostream> #include <stdexcept> #include <string> #include <variant> struct Demo { Demo(int) {} Demo(const Demo&) { throw std::domain_error("copy ctor"); } Demo& operator= (const Demo&) = default; }; int main() { std::variant<std::string, Demo> var{"str"}; assert(var.index() == 0); assert(std::get<0>(var) == "str"); assert(var.valueless_by_exception() == false); try { var = Demo{555}; } catch (const std::domain_error& ex) { std::cout << "1) Exception: " << ex.what() << '\n'; } assert(var.index() == std::variant_npos); assert(var.valueless_by_exception() == true); // Now the var is "valueless" which is an invalid state caused // by an exception raised in the process of type-changing assignment. try { std::get<1>(var); } catch (const std::bad_variant_access& ex) { std::cout << "2) Exception: " << ex.what() << '\n'; } var = "str2"; assert(var.index() == 0); assert(std::get<0>(var) == "str2"); assert(var.valueless_by_exception() == false); }
可能的输出
1) Exception: copy ctor 2) Exception: std::get: variant is valueless
[编辑] 参阅
(C++17) |
根据索引或类型(如果类型唯一)读取变体的值,出错时抛出异常 (函数模板) |
返回 variant 所持可选类型的零基索引(公共成员函数) | |
(C++17) |
对 variant 的值进行无效访问时抛出的异常(类) |