monostate.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___VARIANT_MONOSTATE_H
  10. #define _LIBCPP___VARIANT_MONOSTATE_H
  11. #include <__compare/ordering.h>
  12. #include <__config>
  13. #include <__functional/hash.h>
  14. #include <cstddef>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. #if _LIBCPP_STD_VER >= 17
  20. struct _LIBCPP_TEMPLATE_VIS monostate {};
  21. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(monostate, monostate) noexcept { return true; }
  22. # if _LIBCPP_STD_VER >= 20
  23. _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
  24. return strong_ordering::equal;
  25. }
  26. # else // _LIBCPP_STD_VER >= 20
  27. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator!=(monostate, monostate) noexcept { return false; }
  28. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<(monostate, monostate) noexcept { return false; }
  29. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>(monostate, monostate) noexcept { return false; }
  30. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<=(monostate, monostate) noexcept { return true; }
  31. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noexcept { return true; }
  32. # endif // _LIBCPP_STD_VER >= 20
  33. template <>
  34. struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
  35. using argument_type = monostate;
  36. using result_type = size_t;
  37. inline _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type&) const _NOEXCEPT {
  38. return 66740831; // return a fundamentally attractive random value.
  39. }
  40. };
  41. #endif // _LIBCPP_STD_VER >= 17
  42. _LIBCPP_END_NAMESPACE_STD
  43. #endif // _LIBCPP___VARIANT_MONOSTATE_H