C++標準ライブラリの直和データ型std::variant<...>
と組み合わせて空の状態を表すstd::monostate
オブジェクトでは、std::hash
によるハッシュ計算がサポートされる。*1
各C++処理系で算出されるハッシュ値の一覧(括弧内は併記コメント/定数名):
- GCC:
-7777
(__magic_monostate_hash)*2 - Clang:
66740831
(return a fundamentally attractive random value.)*3 - MSVC:
1729
(Arbitrary value)*4
C++20 20.7.8, 20.7.12より引用。
struct monostate{};The class
monostate
can serve as a first alternative type for avariant
to make thevariant
type default constructible.
template<class... Types> struct hash<variant<Types...>>;The specialization
hash<variant<Types...>>
is enabled (20.14.18) if and only if every specialization inhash<remove_const_t<Types>>...
is enabled. The member functions are not guaranteed to be noexcept.template<> struct hash<monostate>;The specialization is enabled (20.14.18).
関連URL
- cppreference: std::hash, std::monostate
- cpprefjp: std::hash, std::monostate
*1:std::hash<std::monostate> 特殊化の存在に気づいた勢いだけで書いた虚無記事。本来の目的は std::variant<std::monostate, Types...> 型のハッシュサポート。
*2:https://github.com/gcc-mirror/gcc/blob/releases/gcc-11.1.0/libstdc++-v3/include/std/variant#L1837
*3:https://github.com/llvm/llvm-project/blob/llvmorg-17-init/libcxx/include/__variant/monostate.h#L56
*4:https://github.com/microsoft/STL/blob/79e80412313b86f9d0f382ad91470292ea2c303c/stl/inc/variant#L1723