[libcxx-commits] [libcxx] [libcxx] Put `std::monostate` in `<utility>` (PR #128373)

Amr Hesham via libcxx-commits libcxx-commits at lists.llvm.org
Sat Feb 22 14:56:03 PST 2025


https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/128373

>From 99cfe6a9d20a5f4233d1df590dd64e34f884eb9b Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sat, 22 Feb 2025 23:13:20 +0100
Subject: [PATCH 1/3] [libcxx] Put `std::monostate` in `<utility>`

---
 libcxx/docs/ReleaseNotes/21.rst               |  1 +
 libcxx/docs/Status/Cxx2cPapers.csv            |  2 +-
 libcxx/include/__cxx03/__utility/monostate.h  | 65 ++++++++++++++++++
 libcxx/include/__utility/monostate.h          | 66 +++++++++++++++++++
 .../utility.monostate.relpos/relops.pass.cpp  | 47 +++++++++++++
 .../utility.monostate/monostate.pass.cpp      | 31 +++++++++
 6 files changed, 211 insertions(+), 1 deletion(-)
 create mode 100644 libcxx/include/__cxx03/__utility/monostate.h
 create mode 100644 libcxx/include/__utility/monostate.h
 create mode 100644 libcxx/test/std/utilities/utility/utility.monostate.relpos/relops.pass.cpp
 create mode 100644 libcxx/test/std/utilities/utility/utility.monostate/monostate.pass.cpp

diff --git a/libcxx/docs/ReleaseNotes/21.rst b/libcxx/docs/ReleaseNotes/21.rst
index 30c6cc5eaf0c2..d1935c8af02e0 100644
--- a/libcxx/docs/ReleaseNotes/21.rst
+++ b/libcxx/docs/ReleaseNotes/21.rst
@@ -40,6 +40,7 @@ Implemented Papers
 
 - N4258: Cleaning-up noexcept in the Library (`Github <https://github.com/llvm/llvm-project/issues/99937>`__)
 - P1361R2: Integration of chrono with text formatting (`Github <https://github.com/llvm/llvm-project/issues/100014>`__)
+- P0472R3: Put std::monostate in <utility> (`Github <https://github.com/llvm/llvm-project/issues/127874`__)
 
 Improvements and New Features
 -----------------------------
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index 1436db6cf2b45..a47cc193382da 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -102,7 +102,7 @@
 "`P3475R2 <https://wg21.link/P3475R2>`__","Defang and deprecate ``memory_order::consume``","2025-02 (Hagenberg)","","",""
 "`P2786R13 <https://wg21.link/P2786R13>`__","Trivial Relocatability For C++26","2025-02 (Hagenberg)","","",""
 "`P3137R3 <https://wg21.link/P3137R3>`__","``views::to_input``","2025-02 (Hagenberg)","","",""
-"`P0472R3 <https://wg21.link/P0472R3>`__","Put ``std::monostate`` in ``<utility>``","2025-02 (Hagenberg)","","",""
+"`P0472R3 <https://wg21.link/P0472R3>`__","Put ``std::monostate`` in ``<utility>``","2025-02 (Hagenberg)","|Complete|","21",""
 "`P3349R1 <https://wg21.link/P3349R1>`__","Converting contiguous iterators to pointers","2025-02 (Hagenberg)","","",""
 "`P3372R3 <https://wg21.link/P3372R3>`__","constexpr containers and adaptors","2025-02 (Hagenberg)","","",""
 "`P3378R2 <https://wg21.link/P3378R2>`__","constexpr exception types","2025-02 (Hagenberg)","","",""
diff --git a/libcxx/include/__cxx03/__utility/monostate.h b/libcxx/include/__cxx03/__utility/monostate.h
new file mode 100644
index 0000000000000..486a18bfa54ca
--- /dev/null
+++ b/libcxx/include/__cxx03/__utility/monostate.h
@@ -0,0 +1,65 @@
+
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___CXX03___UTILITY_MONOSTATE_H
+#define _LIBCPP___CXX03___UTILITY_MONOSTATE_H
+
+#include <__cxx03/__compare/ordering.h>
+#include <__cxx03/__config>
+#include <__cxx03/__functional/hash.h>
+#include <__cxx03/cstddef>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 17
+
+struct _LIBCPP_TEMPLATE_VIS monostate {};
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(monostate, monostate) noexcept { return true; }
+
+#  if _LIBCPP_STD_VER >= 20
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
+  return strong_ordering::equal;
+}
+
+#  else // _LIBCPP_STD_VER >= 20
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator!=(monostate, monostate) noexcept { return false; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<(monostate, monostate) noexcept { return false; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>(monostate, monostate) noexcept { return false; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<=(monostate, monostate) noexcept { return true; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noexcept { return true; }
+
+#  endif // _LIBCPP_STD_VER >= 20
+
+template <>
+struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
+  using argument_type = monostate;
+  using result_type   = size_t;
+
+  inline _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type&) const _NOEXCEPT {
+    return 66740831; // return a fundamentally attractive random value.
+  }
+};
+
+#endif // _LIBCPP_STD_VER >= 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___CXX03___UTILITY_MONOSTATE_H
diff --git a/libcxx/include/__utility/monostate.h b/libcxx/include/__utility/monostate.h
new file mode 100644
index 0000000000000..3a2a659228e1c
--- /dev/null
+++ b/libcxx/include/__utility/monostate.h
@@ -0,0 +1,66 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___UTILITY_MONOSTATE_H
+#define _LIBCPP___UTILITY_MONOSTATE_H
+
+#include <__compare/ordering.h>
+#include <__configUTILITY>
+#include <__cstddef/size_t.h>
+#include <__functional/hash.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 17
+
+struct _LIBCPP_TEMPLATE_VIS monostate {};
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(monostate, monostate) noexcept { return true; }
+
+#  if _LIBCPP_STD_VER >= 20
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
+  return strong_ordering::equal;
+}
+
+#  else // _LIBCPP_STD_VER >= 20
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator!=(monostate, monostate) noexcept { return false; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<(monostate, monostate) noexcept { return false; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>(monostate, monostate) noexcept { return false; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<=(monostate, monostate) noexcept { return true; }
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noexcept { return true; }
+
+#  endif // _LIBCPP_STD_VER >= 20
+
+template <>
+struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
+#  if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
+  using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = monostate;
+  using result_type _LIBCPP_DEPRECATED_IN_CXX17   = size_t;
+#  endif
+
+  inline _LIBCPP_HIDE_FROM_ABI size_t operator()(const monostate&) const noexcept {
+    return 66740831; // return a fundamentally attractive random value.
+  }
+};
+
+#endif // _LIBCPP_STD_VER >= 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___UTILITY_MONOSTATE_H
diff --git a/libcxx/test/std/utilities/utility/utility.monostate.relpos/relops.pass.cpp b/libcxx/test/std/utilities/utility/utility.monostate.relpos/relops.pass.cpp
new file mode 100644
index 0000000000000..f29d89acdc739
--- /dev/null
+++ b/libcxx/test/std/utilities/utility/utility.monostate.relpos/relops.pass.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <utility>
+
+// constexpr bool operator<(monostate, monostate) noexcept { return false; }
+// constexpr bool operator>(monostate, monostate) noexcept { return false; }
+// constexpr bool operator<=(monostate, monostate) noexcept { return true; }
+// constexpr bool operator>=(monostate, monostate) noexcept { return true; }
+// constexpr bool operator==(monostate, monostate) noexcept { return true; }
+// constexpr bool operator!=(monostate, monostate) noexcept { return false; }
+// constexpr strong_ordering operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; } // since C++20
+
+#include <cassert>
+#include <utility>
+
+#include "test_comparisons.h"
+#include "test_macros.h"
+
+constexpr bool test() {
+  using M = std::monostate;
+  constexpr M m1{};
+  constexpr M m2{};
+  assert(testComparisons(m1, m2, /*isEqual*/ true, /*isLess*/ false));
+  AssertComparisonsAreNoexcept<M>();
+
+#if TEST_STD_VER > 17
+  assert(testOrder(m1, m2, std::strong_ordering::equal));
+  AssertOrderAreNoexcept<M>();
+#endif // TEST_STD_VER > 17
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/test/std/utilities/utility/utility.monostate/monostate.pass.cpp b/libcxx/test/std/utilities/utility/utility.monostate/monostate.pass.cpp
new file mode 100644
index 0000000000000..c78ce5a5c2f18
--- /dev/null
+++ b/libcxx/test/std/utilities/utility/utility.monostate/monostate.pass.cpp
@@ -0,0 +1,31 @@
+
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <utility>
+
+// struct monostate {};
+
+#include <type_traits>
+#include <utility>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+  using M = std::monostate;
+  static_assert(std::is_trivially_default_constructible<M>::value, "");
+  static_assert(std::is_trivially_copy_constructible<M>::value, "");
+  static_assert(std::is_trivially_copy_assignable<M>::value, "");
+  static_assert(std::is_trivially_destructible<M>::value, "");
+  constexpr M m{};
+  ((void)m);
+
+  return 0;
+}

>From bf0ec2016770ce0adb0339521c99db2529061970 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sat, 22 Feb 2025 23:52:04 +0100
Subject: [PATCH 2/3] Add utility/monostate.h to cmakelist

---
 libcxx/include/CMakeLists.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 214c9b1926fb2..9964e1b795ff3 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -911,6 +911,7 @@ set(files
   __utility/swap.h
   __utility/to_underlying.h
   __utility/unreachable.h
+  __utility/monostate.h
   __variant/monostate.h
   __vector/comparison.h
   __vector/container_traits.h
@@ -1938,6 +1939,7 @@ set(files
   __cxx03/__utility/swap.h
   __cxx03/__utility/to_underlying.h
   __cxx03/__utility/unreachable.h
+  __cxx03/__utility/monostate.h
   __cxx03/__variant/monostate.h
   __cxx03/__verbose_abort
   __cxx03/algorithm

>From 9c495564e362eb23111da5bf51206194b7835982 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sat, 22 Feb 2025 23:55:42 +0100
Subject: [PATCH 3/3] Fix hyperlink in release note

---
 libcxx/docs/ReleaseNotes/21.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/docs/ReleaseNotes/21.rst b/libcxx/docs/ReleaseNotes/21.rst
index d1935c8af02e0..019af3bbed8e2 100644
--- a/libcxx/docs/ReleaseNotes/21.rst
+++ b/libcxx/docs/ReleaseNotes/21.rst
@@ -40,7 +40,7 @@ Implemented Papers
 
 - N4258: Cleaning-up noexcept in the Library (`Github <https://github.com/llvm/llvm-project/issues/99937>`__)
 - P1361R2: Integration of chrono with text formatting (`Github <https://github.com/llvm/llvm-project/issues/100014>`__)
-- P0472R3: Put std::monostate in <utility> (`Github <https://github.com/llvm/llvm-project/issues/127874`__)
+- P0472R3: Put std::monostate in <utility> (`Github <https://github.com/llvm/llvm-project/issues/127874>`__)
 
 Improvements and New Features
 -----------------------------



More information about the libcxx-commits mailing list