yohhoyの日記

技術的メモをしていきたい日記

std::monostateのハッシュ値

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 a variant to make the variant 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 in hash<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