From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Define C++23 std::forward_like (P2445R1)
Date: Mon, 25 Sep 2023 09:53:24 +0100 [thread overview]
Message-ID: <20230925085358.2744013-1-jwakely@redhat.com> (raw)
Tested x86_64-linux. Pushed to trunk.
-- >8 --
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 | 26 ++++++++
libstdc++-v3/include/bits/version.def | 8 +++
libstdc++-v3/include/bits/version.h | 27 ++++++---
libstdc++-v3/include/std/utility | 5 +-
.../testsuite/20_util/forward_like/1.cc | 59 +++++++++++++++++++
.../testsuite/20_util/forward_like/2_neg.cc | 10 ++++
.../testsuite/20_util/forward_like/version.cc | 10 ++++
7 files changed, 135 insertions(+), 10 deletions(-)
create mode 100644 libstdc++-v3/testsuite/20_util/forward_like/1.cc
create mode 100644 libstdc++-v3/testsuite/20_util/forward_like/2_neg.cc
create mode 100644 libstdc++-v3/testsuite/20_util/forward_like/version.cc
diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h
index 00997d6f1fb..0151d78aff9 100644
--- a/libstdc++-v3/include/bits/move.h
+++ b/libstdc++-v3/include/bits/move.h
@@ -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.
diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index 6252f5478e0..8f008f9048f 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -1542,6 +1542,14 @@ ftms = {
};
};
+ftms = {
+ name = forward_like;
+ values = {
+ v = 202207;
+ cxxmin = 23;
+ };
+};
+
ftms = {
name = ios_noreplace;
values = {
diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility
index f30e802a88d..bdaf5d4c31b 100644
--- a/libstdc++-v3/include/std/utility
+++ b/libstdc++-v3/include/std/utility
@@ -68,9 +68,10 @@
#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
index 00000000000..928e60094a3
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/forward_like/1.cc
@@ -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
index 00000000000..ff835af1915
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/forward_like/2_neg.cc
@@ -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
index 00000000000..058dccfabe2
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/forward_like/version.cc
@@ -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
--
2.41.0
reply other threads:[~2023-09-25 8:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230925085358.2744013-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).