]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Move std::monostate to <utility> for C++26 (P0472R2)
authorJonathan Wakely <jwakely@redhat.com>
Mon, 25 Nov 2024 13:52:19 +0000 (13:52 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Sat, 30 Nov 2024 21:39:16 +0000 (21:39 +0000)
Another C++26 paper just approved in Wrocław. The std::monostate class
is defined in <variant> since C++17, but for C++26 it should also be
available in <utility>.

libstdc++-v3/ChangeLog:

* include/Makefile.am: Add bits/monostate.h.
* include/Makefile.in: Regenerate.
* include/std/utility: Include <bits/monostate.h>.
* include/std/variant (monostate, hash<monostate>): Move
definitions to ...
* include/bits/monostate.h: New file.
* testsuite/20_util/headers/utility/synopsis.cc: Add monostate
and hash<monostate> declarations.
* testsuite/20_util/monostate/requirements.cc: New test.

libstdc++-v3/include/Makefile.am
libstdc++-v3/include/Makefile.in
libstdc++-v3/include/bits/monostate.h [new file with mode: 0644]
libstdc++-v3/include/std/utility
libstdc++-v3/include/std/variant
libstdc++-v3/testsuite/20_util/headers/utility/synopsis.cc
libstdc++-v3/testsuite/20_util/monostate/requirements.cc [new file with mode: 0644]

index 422a0f4bd0a8b7e6d1d0e7d574462b849a1b7eb1..6efd3cd5f1c8032c7f74031c014d030ea9e90d06 100644 (file)
@@ -133,6 +133,7 @@ bits_freestanding = \
        ${bits_srcdir}/iterator_concepts.h \
        ${bits_srcdir}/max_size_type.h \
        ${bits_srcdir}/memoryfwd.h \
+       ${bits_srcdir}/monostate.h \
        ${bits_srcdir}/move.h \
        ${bits_srcdir}/out_ptr.h \
        ${bits_srcdir}/predefined_ops.h \
index 9fd4ab4848c365db7e9ed3e74509c7e682237634..3b5f93ce185d4a5430b3e74a2ed4ef93a40ac2e7 100644 (file)
@@ -488,6 +488,7 @@ bits_freestanding = \
        ${bits_srcdir}/iterator_concepts.h \
        ${bits_srcdir}/max_size_type.h \
        ${bits_srcdir}/memoryfwd.h \
+       ${bits_srcdir}/monostate.h \
        ${bits_srcdir}/move.h \
        ${bits_srcdir}/out_ptr.h \
        ${bits_srcdir}/predefined_ops.h \
diff --git a/libstdc++-v3/include/bits/monostate.h b/libstdc++-v3/include/bits/monostate.h
new file mode 100644 (file)
index 0000000..b6da720
--- /dev/null
@@ -0,0 +1,78 @@
+// Definition of std::monostate for <variant> and <utility> -*- C++ -*-
+
+// Copyright (C) 2016-2024 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file bits/monostate.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{utility}
+ */
+
+#ifndef _GLIBCXX_MONOSTATE_H
+#define _GLIBCXX_MONOSTATE_H 1
+
+#include <bits/version.h>
+
+#ifdef __glibcxx_variant // C++ >= 17
+
+#include <bits/functional_hash.h>
+#if __cplusplus >= 202002L
+# include <compare>
+#endif
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+  struct monostate { };
+
+  constexpr bool operator==(monostate, monostate) noexcept { return true; }
+#ifdef __cpp_lib_three_way_comparison
+  constexpr strong_ordering
+  operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
+#else
+  constexpr bool operator!=(monostate, monostate) noexcept { return false; }
+  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; }
+#endif
+
+  template<>
+    struct hash<monostate>
+    {
+#if __cplusplus < 202002L
+      using result_type [[__deprecated__]] = size_t;
+      using argument_type [[__deprecated__]] = monostate;
+#endif
+
+      size_t
+      operator()(const monostate&) const noexcept
+      {
+       constexpr size_t __magic_monostate_hash = -7777;
+       return __magic_monostate_hash;
+      }
+    };
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
+#endif // __glibcxx_variant
+#endif /* _GLIBCXX_MONOSTATE_H */
index 877e9a85c7170ccc1fa4cd9a27de7b9e85a6985d..6b2943c821b31f58798d4fae41cb992d43454e8f 100644 (file)
 #include <ext/numeric_traits.h> // __is_standard_integer, __int_traits
 #endif
 
+#if __cplusplus > 202302L
+# include <bits/monostate.h>
+#endif
+
 #define __glibcxx_want_addressof_constexpr
 #define __glibcxx_want_as_const
 #define __glibcxx_want_constexpr_algorithms
index 32e539980839c72ec18ab138ffead0fd1f9f3951..f46b553d78e4e12f4ecfbf4d121f086d8140652a 100644 (file)
@@ -45,6 +45,7 @@
 #include <bits/exception_defines.h>
 #include <bits/functional_hash.h>
 #include <bits/invoke.h>
+#include <bits/monostate.h>
 #include <bits/parse_numbers.h> // _Select_int
 #include <bits/stl_iterator_base_funcs.h>
 #include <bits/stl_construct.h>
@@ -65,7 +66,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename... _Types> class tuple;
   template<typename... _Types> class variant;
-  template <typename> struct hash;
 
   template<typename _Variant>
     struct variant_size;
@@ -1232,8 +1232,6 @@ namespace __variant
       return std::get_if<__n>(__ptr);
     }
 
-  struct monostate { };
-
 namespace __detail::__variant
 {
   template<typename _Ret, typename _Vp, typename _Op>
@@ -1354,8 +1352,6 @@ namespace __detail::__variant
                                  });
     }
 
-  constexpr bool operator==(monostate, monostate) noexcept { return true; }
-
 #ifdef __cpp_lib_three_way_comparison
   template<typename... _Types>
     requires (three_way_comparable<_Types> && ...)
@@ -1371,15 +1367,6 @@ namespace __detail::__variant
                                    return __l <=> __r;
                                  });
     }
-
-  constexpr strong_ordering
-  operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; }
-#else
-  constexpr bool operator!=(monostate, monostate) noexcept { return false; }
-  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; }
 #endif
 
   template<typename _Visitor, typename... _Variants>
@@ -2030,22 +2017,6 @@ namespace __detail::__variant
 #endif
     { };
 
-  template<>
-    struct hash<monostate>
-    {
-#if __cplusplus < 202002L
-      using result_type [[__deprecated__]] = size_t;
-      using argument_type [[__deprecated__]] = monostate;
-#endif
-
-      size_t
-      operator()(const monostate&) const noexcept
-      {
-       constexpr size_t __magic_monostate_hash = -7777;
-       return __magic_monostate_hash;
-      }
-    };
-
   template<typename... _Types>
     struct __is_fast_hash<hash<variant<_Types...>>>
     : bool_constant<(__is_fast_hash<_Types>::value && ...)>
index 51e88b70f5149e1a56c42c294e9bfedcdfaebf1c..73c3b51fa44c20bf662af9092ac66cf0af64557f 100644 (file)
@@ -137,6 +137,11 @@ namespace std {
   struct in_place_t;
   template<class> struct in_place_type_t;
   template<size_t> struct in_place_index_t;
+
+#if __cplusplus > 202302L
+  struct monostate;
+  template<> struct hash<monostate>;
+#endif
 #endif
 #endif
 }
diff --git a/libstdc++-v3/testsuite/20_util/monostate/requirements.cc b/libstdc++-v3/testsuite/20_util/monostate/requirements.cc
new file mode 100644 (file)
index 0000000..c385862
--- /dev/null
@@ -0,0 +1,38 @@
+// { dg-do compile { target c++17 } }
+
+#if __cplusplus <= 202302L
+# include <variant>
+#else
+# include <utility>
+#endif
+
+void
+test01()
+{
+  static_assert( std::is_nothrow_default_constructible_v<std::monostate> );
+  static_assert( std::is_nothrow_copy_constructible_v<std::monostate> );
+  static_assert( std::is_nothrow_copy_assignable_v<std::monostate> );
+  static_assert( std::is_nothrow_destructible_v<std::monostate> );
+}
+
+void
+test02()
+{
+#ifdef __cpp_lib_three_way_comparison
+  static_assert( std::is_eq(std::monostate{} <=> std::monostate{}) );
+#endif
+  static_assert( std::monostate{} == std::monostate{} );
+  static_assert( std::monostate{} <= std::monostate{} );
+  static_assert( std::monostate{} >= std::monostate{} );
+  static_assert( !(std::monostate{} != std::monostate{}) );
+  static_assert( !(std::monostate{} < std::monostate{}) );
+  static_assert( !(std::monostate{} > std::monostate{}) );
+}
+
+void
+test03()
+{
+  std::monostate m;
+  std::hash<std::monostate> h;
+  static_assert( std::is_same_v<decltype(h(m)), std::size_t> );
+}