std:: hash <std::optional>
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
| Iterators | ||||
|
(C++26)
|
||||
|
(C++26)
|
||||
| Monadic operations | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Modifiers | ||||
| Non-member functions | ||||
| Deduction guides | ||||
| Helper classes | ||||
|
hash
<std::optional>
|
||||
| Helper objects | ||||
|
ヘッダーで定義
<optional>
|
||
|
template
<
class
T
>
struct hash < std:: optional < T >> ; |
(C++17以降) | |
std::hash
の
std::optional
クラスに対するテンプレート特殊化は、ユーザーが
optional
オブジェクトに含まれる値のハッシュを取得することを可能にします。
std::hash
<
std::
optional
<
T
>>
の特殊化は、
std::
hash
<
std::
remove_const_t
<
T
>>
が有効な場合に有効化され(
std::hash
を参照)、それ以外の場合は無効化されます。
有効な場合、値を持つ型
std::
optional
<
T
>
のオブジェクト
o
に対して、
std::hash
<
std::
optional
<
T
>>
(
)
(
o
)
は
std::
hash
<
std::
remove_const_t
<
T
>>
(
)
(
*
o
)
と同じ値に評価されます。値を持たないoptionalの場合、ハッシュ値は未規定です。
この特殊化のメンバー関数は、基になる型のハッシュが例外をスローする可能性があるため、noexcept であることが保証されていません。
テンプレートパラメータ
| T | - |
optional
オブジェクトに含まれる値の型
|
例
#include <iostream> #include <optional> #include <string> #include <unordered_set> using namespace std::literals; int main() { using OptStr = std::optional<std::string>; // hash<optional>によりunordered_setの使用が可能になる std::unordered_set<OptStr> s = { "ABC"s, "abc"s, std::nullopt, "def"s }; for (const auto& o : s) std::cout << o.value_or("(null)") << '\t' << std::hash<OptStr>{}(o) << '\n'; }
出力例:
def 11697390762615875584 (null) 18446744073709548283 abc 3663726644998027833 ABC 11746482041453314842
関連項目
|
(C++11)
|
ハッシュ関数オブジェクト
(クラステンプレート) |