]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Define C++23 std::forward_like (P2445R1)
authorJonathan Wakely <jwakely@redhat.com>
Tue, 19 Sep 2023 13:37:15 +0000 (14:37 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 25 Sep 2023 08:48:44 +0000 (09:48 +0100)
libstdc++-v3/ChangeLog:

* include/bits/move.h (forward_list): Define for C++23.
* include/bits/version.def (forward_like): Define.
* include/bits/version.h: Regenerate.
* include/std/utility (__glibcxx_want_forward_like): Define.
* testsuite/20_util/forward_like/1.cc: New test.
* testsuite/20_util/forward_like/2_neg.cc: New test.
* testsuite/20_util/forward_like/version.cc: New test.

libstdc++-v3/include/bits/move.h
libstdc++-v3/include/bits/version.def
libstdc++-v3/include/bits/version.h
libstdc++-v3/include/std/utility
libstdc++-v3/testsuite/20_util/forward_like/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/forward_like/2_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/forward_like/version.cc [new file with mode: 0644]

index 00997d6f1fb6de24d26c67fb5923bf20c38014cf..0151d78aff93ba7a98ed745090fb2263b57d45c5 100644 (file)
@@ -89,6 +89,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return static_cast<_Tp&&>(__t);
     }
 
+#if __glibcxx_forward_like // C++ >= 23
+  template<typename _Tp, typename _Up>
+  [[nodiscard]]
+  constexpr decltype(auto)
+  forward_like(_Up&& __x) noexcept
+  {
+    constexpr bool __as_rval = is_rvalue_reference_v<_Tp&&>;
+
+    if constexpr (is_const_v<remove_reference_t<_Tp>>)
+      {
+       using _Up2 = remove_reference_t<_Up>;
+       if constexpr (__as_rval)
+         return static_cast<const _Up2&&>(__x);
+       else
+         return static_cast<const _Up2&>(__x);
+      }
+    else
+      {
+       if constexpr (__as_rval)
+         return static_cast<remove_reference_t<_Up>&&>(__x);
+       else
+         return static_cast<_Up&>(__x);
+      }
+  }
+#endif
+
   /**
    *  @brief  Convert a value to an rvalue.
    *  @param  __t  A thing of arbitrary type.
index 6252f5478e0a893a64a7b4c8772c31479e76ea47..8f008f9048fc59b24fb7133ac771c366a3b45f7b 100644 (file)
@@ -1542,6 +1542,14 @@ ftms = {
   };
 };
 
+ftms = {
+  name = forward_like;
+  values = {
+    v = 202207;
+    cxxmin = 23;
+  };
+};
+
 ftms = {
   name = ios_noreplace;
   values = {
index 782198acc0c92a2eaa9087bd399d56d2c7595b17..b6cb8906b4dc89832c075caf0c5b0b07fbe09973 100644 (file)
 #undef __glibcxx_want_formatters
 
 // from version.def line 1546
+#if !defined(__cpp_lib_forward_like)
+# if (__cplusplus >= 202302L)
+#  define __glibcxx_forward_like 202207L
+#  if defined(__glibcxx_want_all) || defined(__glibcxx_want_forward_like)
+#   define __cpp_lib_forward_like 202207L
+#  endif
+# endif
+#endif /* !defined(__cpp_lib_forward_like) && defined(__glibcxx_want_forward_like) */
+#undef __glibcxx_want_forward_like
+
+// from version.def line 1554
 #if !defined(__cpp_lib_ios_noreplace)
 # if (__cplusplus >= 202302L) && _GLIBCXX_HOSTED
 #  define __glibcxx_ios_noreplace 202207L
 #endif /* !defined(__cpp_lib_ios_noreplace) && defined(__glibcxx_want_ios_noreplace) */
 #undef __glibcxx_want_ios_noreplace
 
-// from version.def line 1555
+// from version.def line 1563
 #if !defined(__cpp_lib_move_only_function)
 # if (__cplusplus >= 202302L) && _GLIBCXX_HOSTED
 #  define __glibcxx_move_only_function 202110L
 #endif /* !defined(__cpp_lib_move_only_function) && defined(__glibcxx_want_move_only_function) */
 #undef __glibcxx_want_move_only_function
 
-// from version.def line 1564
+// from version.def line 1572
 #if !defined(__cpp_lib_spanstream)
 # if (__cplusplus >= 202302L) && _GLIBCXX_HOSTED && (__glibcxx_span)
 #  define __glibcxx_spanstream 202106L
 #endif /* !defined(__cpp_lib_spanstream) && defined(__glibcxx_want_spanstream) */
 #undef __glibcxx_want_spanstream
 
-// from version.def line 1574
+// from version.def line 1582
 #if !defined(__cpp_lib_stacktrace)
 # if (__cplusplus >= 202302L) && _GLIBCXX_HOSTED && (_GLIBCXX_HAVE_STACKTRACE)
 #  define __glibcxx_stacktrace 202011L
 #endif /* !defined(__cpp_lib_stacktrace) && defined(__glibcxx_want_stacktrace) */
 #undef __glibcxx_want_stacktrace
 
-// from version.def line 1584
+// from version.def line 1592
 #if !defined(__cpp_lib_string_contains)
 # if (__cplusplus >= 202302L) && _GLIBCXX_HOSTED
 #  define __glibcxx_string_contains 202011L
 #endif /* !defined(__cpp_lib_string_contains) && defined(__glibcxx_want_string_contains) */
 #undef __glibcxx_want_string_contains
 
-// from version.def line 1593
+// from version.def line 1601
 #if !defined(__cpp_lib_string_resize_and_overwrite)
 # if (__cplusplus >= 202302L) && _GLIBCXX_HOSTED
 #  define __glibcxx_string_resize_and_overwrite 202110L
 #endif /* !defined(__cpp_lib_string_resize_and_overwrite) && defined(__glibcxx_want_string_resize_and_overwrite) */
 #undef __glibcxx_want_string_resize_and_overwrite
 
-// from version.def line 1602
+// from version.def line 1610
 #if !defined(__cpp_lib_fstream_native_handle)
 # if (__cplusplus >  202302L) && _GLIBCXX_HOSTED
 #  define __glibcxx_fstream_native_handle 202306L
 #endif /* !defined(__cpp_lib_fstream_native_handle) && defined(__glibcxx_want_fstream_native_handle) */
 #undef __glibcxx_want_fstream_native_handle
 
-// from version.def line 1611
+// from version.def line 1619
 #if !defined(__cpp_lib_ratio)
 # if (__cplusplus >  202302L)
 #  define __glibcxx_ratio 202306L
 #endif /* !defined(__cpp_lib_ratio) && defined(__glibcxx_want_ratio) */
 #undef __glibcxx_want_ratio
 
-// from version.def line 1619
+// from version.def line 1627
 #if !defined(__cpp_lib_to_string)
 # if (__cplusplus >  202302L) && _GLIBCXX_HOSTED && (__glibcxx_to_chars)
 #  define __glibcxx_to_string 202306L
index f30e802a88dbd8d4d25f02c8dbe35a48d200f8fb..bdaf5d4c31b9dee98468e5b0500bd49a97f8292e 100644 (file)
 #include <bits/stl_relops.h>
 #include <bits/stl_pair.h>
 
-#define __glibcxx_want_exchange_function
-#define __glibcxx_want_constexpr_algorithms
 #define __glibcxx_want_as_const
+#define __glibcxx_want_constexpr_algorithms
+#define __glibcxx_want_exchange_function
+#define __glibcxx_want_forward_like
 #define __glibcxx_want_integer_comparison_functions
 #define __glibcxx_want_to_underlying
 #define __glibcxx_want_unreachable
diff --git a/libstdc++-v3/testsuite/20_util/forward_like/1.cc b/libstdc++-v3/testsuite/20_util/forward_like/1.cc
new file mode 100644 (file)
index 0000000..928e600
--- /dev/null
@@ -0,0 +1,59 @@
+// { dg-do compile { target c++23 } }
+// { dg-add-options no_pch }
+
+#include <utility>
+
+#ifndef __cpp_lib_forward_like
+# error "Feature-test macro for forward_like missing in <utility>"
+#elif __cpp_lib_forward_like != 202207L
+# error "Feature-test macro for forward_like has wrong value in <utility>"
+#endif
+
+template<typename T, typename U>
+using forward_like_t = decltype(std::forward_like<T>(std::declval<U>()));
+
+#if 0
+using std::is_same_v;
+#else
+#include <concepts>
+template<class T, class U> concept is_same_v = std::same_as<T, U>;
+#endif
+
+static_assert( is_same_v<forward_like_t<int, long>, long&&> );
+static_assert( is_same_v<forward_like_t<int&, long>, long&> );
+static_assert( is_same_v<forward_like_t<int&&, long>, long&&> );
+
+static_assert( is_same_v<forward_like_t<const int, long>, const long&&> );
+static_assert( is_same_v<forward_like_t<const int&, long>, const long&> );
+static_assert( is_same_v<forward_like_t<const int&&, long>, const long&&> );
+
+static_assert( is_same_v<forward_like_t<int, const long>, const long&&> );
+static_assert( is_same_v<forward_like_t<int&, const long>, const long&> );
+static_assert( is_same_v<forward_like_t<int&&, const long>, const long&&> );
+
+static_assert( is_same_v<forward_like_t<const int, long&>, const long&&> );
+static_assert( is_same_v<forward_like_t<const int&, long&>, const long&> );
+static_assert( is_same_v<forward_like_t<const int&&, long&>, const long&&> );
+
+static_assert( is_same_v<forward_like_t<int, const long&>, const long&&> );
+static_assert( is_same_v<forward_like_t<int&, const long&>, const long&> );
+static_assert( is_same_v<forward_like_t<int&&, const long&>, const long&&> );
+
+static_assert( is_same_v<forward_like_t<const int, long&&>, const long&&> );
+static_assert( is_same_v<forward_like_t<const int&, long&&>, const long&> );
+static_assert( is_same_v<forward_like_t<const int&&, long&&>, const long&&> );
+
+static_assert( is_same_v<forward_like_t<int, const long&&>, const long&&> );
+static_assert( is_same_v<forward_like_t<int&, const long&&>, const long&> );
+static_assert( is_same_v<forward_like_t<int&&, const long&&>, const long&&> );
+
+static_assert( is_same_v<forward_like_t<volatile int, long>, long&&> );
+static_assert( is_same_v<forward_like_t<volatile int&, long>, long&> );
+static_assert( is_same_v<forward_like_t<volatile int&&, long>, long&&> );
+
+static_assert( is_same_v<forward_like_t<const int, volatile long>,
+                        const volatile long&&> );
+static_assert( is_same_v<forward_like_t<const int&, volatile long>,
+                        const volatile long&> );
+static_assert( is_same_v<forward_like_t<const int&&, volatile long>,
+                        const volatile long&&> );
diff --git a/libstdc++-v3/testsuite/20_util/forward_like/2_neg.cc b/libstdc++-v3/testsuite/20_util/forward_like/2_neg.cc
new file mode 100644 (file)
index 0000000..ff835af
--- /dev/null
@@ -0,0 +1,10 @@
+// { dg-do compile { target c++23 } }
+
+#include <utility>
+
+auto x1 = std::forward_like<void>(1); // { dg-error "here" }
+// { dg-error "forming reference to void" "" { target *-*-* } 0 }
+auto x2 = std::forward_like<void()const>(1); // { dg-error "here" }
+// { dg-error "forming reference to qualified function" "" { target *-*-* } 0 }
+
+// { dg-prune-output "inconsistent deduction for auto return type" } // PR111484
diff --git a/libstdc++-v3/testsuite/20_util/forward_like/version.cc b/libstdc++-v3/testsuite/20_util/forward_like/version.cc
new file mode 100644 (file)
index 0000000..058dccf
--- /dev/null
@@ -0,0 +1,10 @@
+// { dg-do preprocess { target c++23 } }
+// { dg-add-options no_pch }
+
+#include <version>
+
+#ifndef __cpp_lib_forward_like
+# error "Feature-test macro for forward_like missing in <version>"
+#elif __cpp_lib_forward_like != 202207L
+# error "Feature-test macro for forward_like has wrong value in <version>"
+#endif