std:: variant_npos
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::variant
| Member functions | ||||
| Observers | ||||
| Modifiers | ||||
| Visitation | ||||
|
(C++26)
|
||||
| Non-member functions | ||||
| Helper classes | ||||
| Helper objects | ||||
|
variant_npos
|
|
定义于头文件
<variant>
|
||
|
inline
constexpr
std::
size_t
variant_npos
=
-
1
;
|
(C++17 起) | |
这是一个特殊值,等于类型 std:: size_t 可表示的最大值,当 valueless_by_exception() 为 true 时,该值作为 index() 的返回值使用。
运行此代码
#include <iostream> #include <stdexcept> #include <string> #include <variant> struct Demon { Demon(int) {} Demon(const Demon&) { throw std::domain_error("copy ctor"); } Demon& operator= (const Demon&) = default; }; int main() { std::variant<int, Demon> var{42}; std::cout << std::boolalpha << "index == npos: " << (var.index() == std::variant_npos) << '\n'; try { var = Demon{666}; } catch (const std::domain_error& ex) { std::cout << "Exception: " << ex.what() << '\n' << "index == npos: " << (var.index() == std::variant_npos) << '\n' << "valueless: " << var.valueless_by_exception() << '\n'; } }
可能的输出:
index == npos: false Exception: copy ctor index == npos: true valueless: true
参见
返回
variant
所持有可选项的从零开始的索引
(公开成员函数) |
|
检查
variant
是否处于无效状态
(公开成员函数) |