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 で表現可能な最大値に等しい特別な値であり、 index() が valueless_by_exception() が true の場合の戻り値として使用されます。
このコードを実行
#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
が保持する代替案の0から始まるインデックスを返す
(public member function) |
|
variant
が無効な状態にあるかどうかをチェックする
(public member function) |