[libcxx-commits] [libcxx] [libc++] Implement LWG3545: std::pointer_traits should be SFINAE-friendly. (PR #65177)
Daniel Cheng via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Sep 7 11:41:25 PDT 2023
https://github.com/zetafunction updated https://github.com/llvm/llvm-project/pull/65177:
>From 0778e3e24eb306cbfc5b5e8246f59ebfdbf52168 Mon Sep 17 00:00:00 2001
From: Daniel Cheng <zetafunction at gmail.com>
Date: Fri, 25 Aug 2023 23:46:50 -0700
Subject: [PATCH] [libc++] Implement LWG3545: std::pointer_traits should be
SFINAE-friendly.
See https://wg21.link/LWG3545 for background and details.
Differential Revision: https://reviews.llvm.org/D158922
---
libcxx/docs/Status/Cxx23Issues.csv | 2 +-
libcxx/include/__memory/pointer_traits.h | 17 +-
.../contiguous_iterator.verify.cpp | 54 -------
.../pointer_traits.pass.cpp | 153 ++++++++++++++++++
.../pointer.conversion/to_address.verify.cpp | 23 +++
...to_address_without_pointer_traits.pass.cpp | 66 ++++++++
6 files changed, 254 insertions(+), 61 deletions(-)
delete mode 100644 libcxx/test/libcxx/iterators/iterator.requirements/iterator.concepts/iterator.concept.random.access/contiguous_iterator.verify.cpp
create mode 100644 libcxx/test/std/utilities/memory/pointer.conversion/pointer_traits.pass.cpp
create mode 100644 libcxx/test/std/utilities/memory/pointer.conversion/to_address.verify.cpp
create mode 100644 libcxx/test/std/utilities/memory/pointer.conversion/to_address_without_pointer_traits.pass.cpp
diff --git a/libcxx/docs/Status/Cxx23Issues.csv b/libcxx/docs/Status/Cxx23Issues.csv
index 2a00289af376924..a0d4b22e20d037b 100644
--- a/libcxx/docs/Status/Cxx23Issues.csv
+++ b/libcxx/docs/Status/Cxx23Issues.csv
@@ -190,7 +190,7 @@
"`3118 <https://wg21.link/LWG3118>`__","``fpos`` equality comparison unspecified", "November 2022","","",""
"`3177 <https://wg21.link/LWG3177>`__","Limit permission to specialize variable templates to program-defined types", "November 2022","|Nothing to do|","",""
"`3515 <https://wg21.link/LWG3515>`__","§[stacktrace.basic.nonmem]: ``operator<<`` should be less templatized", "November 2022","","",""
-"`3545 <https://wg21.link/LWG3545>`__","``std::pointer_traits`` should be SFINAE-friendly", "November 2022","","",""
+"`3545 <https://wg21.link/LWG3545>`__","``std::pointer_traits`` should be SFINAE-friendly", "November 2022","|Complete|","18.0",""
"`3569 <https://wg21.link/LWG3569>`__","``join_view`` fails to support ranges of ranges with non-default_initializable iterators", "November 2022","","","|ranges|"
"`3594 <https://wg21.link/LWG3594>`__","``inout_ptr`` — inconsistent ``release()`` in destructor", "November 2022","","",""
"`3597 <https://wg21.link/LWG3597>`__","Unsigned integer types don't model advanceable", "November 2022","","","|ranges|"
diff --git a/libcxx/include/__memory/pointer_traits.h b/libcxx/include/__memory/pointer_traits.h
index c33e7bd43f29f7d..7617948ed76bd66 100644
--- a/libcxx/include/__memory/pointer_traits.h
+++ b/libcxx/include/__memory/pointer_traits.h
@@ -35,7 +35,7 @@ template <class _Tp>
struct __has_element_type<_Tp, __void_t<typename _Tp::element_type> > : true_type {};
template <class _Ptr, bool = __has_element_type<_Ptr>::value>
-struct __pointer_traits_element_type;
+struct __pointer_traits_element_type {};
template <class _Ptr>
struct __pointer_traits_element_type<_Ptr, true>
@@ -111,12 +111,14 @@ struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
typedef _Sp<_Up, _Args...> type;
};
+template <class _Ptr, class = void>
+struct __pointer_traits_impl {};
+
template <class _Ptr>
-struct _LIBCPP_TEMPLATE_VIS pointer_traits
-{
- typedef _Ptr pointer;
- typedef typename __pointer_traits_element_type<pointer>::type element_type;
- typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
+struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > {
+ typedef _Ptr pointer;
+ typedef typename __pointer_traits_element_type<pointer>::type element_type;
+ typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
#ifndef _LIBCPP_CXX03_LANG
template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
@@ -133,6 +135,9 @@ struct _LIBCPP_TEMPLATE_VIS pointer_traits
{return pointer::pointer_to(__r);}
};
+template <class _Ptr>
+struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};
+
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
{
diff --git a/libcxx/test/libcxx/iterators/iterator.requirements/iterator.concepts/iterator.concept.random.access/contiguous_iterator.verify.cpp b/libcxx/test/libcxx/iterators/iterator.requirements/iterator.concepts/iterator.concept.random.access/contiguous_iterator.verify.cpp
deleted file mode 100644
index 37c5ad9610a60cb..000000000000000
--- a/libcxx/test/libcxx/iterators/iterator.requirements/iterator.concepts/iterator.concept.random.access/contiguous_iterator.verify.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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
-
-// This test checks that std::contiguous_iterator uses std::to_address, which is not SFINAE-friendly
-// when the type is missing the `T::element_type` typedef.
-
-#include <iterator>
-
-#include <compare>
-#include <cstddef>
-
-struct no_element_type {
- typedef std::contiguous_iterator_tag iterator_category;
- typedef int value_type;
- typedef std::ptrdiff_t difference_type;
- typedef int* pointer;
- typedef int& reference;
- typedef no_element_type self;
-
- no_element_type();
-
- reference operator*() const;
- pointer operator->() const;
- auto operator<=>(const self&) const = default;
-
- self& operator++();
- self operator++(int);
-
- self& operator--();
- self operator--(int);
-
- self& operator+=(difference_type n);
- self operator+(difference_type n) const;
- friend self operator+(difference_type n, self x);
-
- self& operator-=(difference_type n);
- self operator-(difference_type n) const;
- difference_type operator-(const self& n) const;
-
- reference operator[](difference_type n) const;
-};
-
-void test() {
- (void) std::contiguous_iterator<no_element_type>;
- // expected-error@*:* {{implicit instantiation of undefined template}}
- // expected-note@*:* {{to_address}}
-}
diff --git a/libcxx/test/std/utilities/memory/pointer.conversion/pointer_traits.pass.cpp b/libcxx/test/std/utilities/memory/pointer.conversion/pointer_traits.pass.cpp
new file mode 100644
index 000000000000000..9a8337d82245e06
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/pointer.conversion/pointer_traits.pass.cpp
@@ -0,0 +1,153 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// template <class T> constexpr T* to_address(T* p) noexcept;
+// template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
+
+#include <memory>
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+
+template <class T, class = void>
+struct HasElementType : std::false_type {};
+
+template <class T>
+struct HasElementType<T, std::void_t<typename std::pointer_traits<T>::element_type>> : std::true_type {};
+
+template <class T, class = void>
+struct HasPointerType : std::false_type {};
+
+template <class T>
+struct HasPointerType<T, std::void_t<typename std::pointer_traits<T>::pointer>> : std::true_type {};
+
+template <class T, class = void>
+struct HasDifferenceType : std::false_type {};
+
+template <class T>
+struct HasDifferenceType<T, std::void_t<typename std::pointer_traits<T>::difference_type>> : std::true_type {};
+
+template <class T, class U, class = void>
+struct HasRebind : std::false_type {};
+
+template <class T, class U>
+struct HasRebind<T, U, std::void_t<typename std::pointer_traits<T>::template rebind<U>>> : std::true_type {};
+
+template <class T, class = void>
+struct HasPointerTo : std::false_type {};
+
+template <class T>
+struct HasPointerTo<T,
+ std::void_t<decltype(std::pointer_traits<T>::pointer_to(
+ std::declval<std::add_lvalue_reference_t<typename std::pointer_traits<T>::element_type>>()))>>
+ : std::true_type {};
+
+struct Irrelevant;
+
+struct NotAPtr {};
+
+struct LongPtr;
+
+struct PtrWithElementType {
+ using element_type = int;
+};
+
+template <class T, class Arg>
+struct TemplatedPtr {
+ template <typename U, typename = std::enable_if_t<std::is_same_v<long, U>>>
+ using rebind = LongPtr;
+ static constexpr auto pointer_to(T&) { return TemplatedPtr(); }
+};
+
+template <class T, class Arg>
+struct TemplatedPtrWithElementType {
+ using element_type = int;
+ template <typename U, typename = std::enable_if_t<std::is_same_v<long, U>>>
+ using rebind = LongPtr;
+ static constexpr auto pointer_to(T&) { return TemplatedPtrWithElementType(); }
+};
+
+constexpr bool test() {
+ {
+ using Ptr = NotAPtr;
+ assert(!HasElementType<Ptr>::value);
+ assert(!HasPointerType<Ptr>::value);
+ assert(!HasDifferenceType<Ptr>::value);
+ assert((!HasRebind<Ptr, long>::value));
+ assert(!HasPointerTo<Ptr>::value);
+ }
+
+ {
+ using Ptr = PtrWithElementType;
+
+ assert(HasElementType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::element_type, int);
+
+ assert(HasPointerType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::pointer, Ptr);
+
+ assert(HasDifferenceType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::difference_type, ptrdiff_t);
+
+ // TODO: Consider supporting SFINAE testing of std::pointer_traits<Ptr>.
+ }
+
+ {
+ using Ptr = TemplatedPtr<int, Irrelevant>;
+
+ assert(HasElementType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::element_type, int);
+
+ assert(HasPointerType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::pointer, Ptr);
+
+ assert(HasDifferenceType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::difference_type, ptrdiff_t);
+
+ assert((HasRebind<Ptr, long>::value));
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::rebind<long>, LongPtr);
+ assert((HasRebind<Ptr, long long>::value));
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::rebind<long long>, TemplatedPtr<long long, Irrelevant>);
+
+ assert(HasPointerTo<Ptr>::value);
+ }
+
+ {
+ using Ptr = TemplatedPtrWithElementType<Irrelevant, Irrelevant>;
+
+ assert(HasElementType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::element_type, int);
+
+ assert(HasPointerType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::pointer, Ptr);
+
+ assert(HasDifferenceType<Ptr>::value);
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::difference_type, ptrdiff_t);
+
+ assert((HasRebind<Ptr, long>::value));
+ ASSERT_SAME_TYPE(typename std::pointer_traits<Ptr>::rebind<long>, LongPtr);
+ assert((HasRebind<Ptr, long long>::value));
+ ASSERT_SAME_TYPE(
+ typename std::pointer_traits<Ptr>::rebind<long long>, TemplatedPtrWithElementType<long long, Irrelevant>);
+
+ assert(HasPointerTo<Ptr>::value);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+ return 0;
+}
diff --git a/libcxx/test/std/utilities/memory/pointer.conversion/to_address.verify.cpp b/libcxx/test/std/utilities/memory/pointer.conversion/to_address.verify.cpp
new file mode 100644
index 000000000000000..8eddbe4c137190e
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/pointer.conversion/to_address.verify.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
+// Mandates: one of pointer_traits<Ptr>::to_address() or Ptr::operator->()
+// is present.
+
+#include <memory>
+
+struct NotPtr {};
+
+void test() {
+ (void)std::to_address(NotPtr()); // expected-error@*:* {{no matching function for call to 'to_address'}}
+}
diff --git a/libcxx/test/std/utilities/memory/pointer.conversion/to_address_without_pointer_traits.pass.cpp b/libcxx/test/std/utilities/memory/pointer.conversion/to_address_without_pointer_traits.pass.cpp
new file mode 100644
index 000000000000000..4d05c10e0fbdc95
--- /dev/null
+++ b/libcxx/test/std/utilities/memory/pointer.conversion/to_address_without_pointer_traits.pass.cpp
@@ -0,0 +1,66 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
+// Should not require a specialization of pointer_traits for Ptr.
+
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+struct IntPtr {
+ constexpr int* operator->() const { return ptr; }
+
+ int* ptr;
+};
+
+template <class T, bool>
+struct TemplatedPtr {
+ constexpr T* operator->() const { return ptr; }
+
+ T* ptr;
+};
+
+template <template <class...> class Templ, class Ignore, class... Args>
+struct is_valid_expansion_impl : std::false_type {};
+
+template <template <class...> class Templ, class... Args>
+struct is_valid_expansion_impl<Templ, decltype((void)Templ<Args...>{}, 0), Args...> : std::true_type {};
+
+template <template <class...> class Templ, class... Args>
+using is_valid_expansion = is_valid_expansion_impl<Templ, int, Args...>;
+
+template <class Ptr>
+using TestToAddressCall = decltype(std::to_address(std::declval<Ptr>()));
+
+constexpr bool test() {
+ int i = 0;
+
+ static_assert(std::to_address(IntPtr{nullptr}) == nullptr);
+ static_assert(std::to_address(IntPtr{&i}) == &i);
+
+ bool b = false;
+
+ static_assert(std::to_address(TemplatedPtr<bool, true>{nullptr}) == nullptr);
+ static_assert(std::to_address(TemplatedPtr<bool, true>{&b}) == &b);
+
+ static_assert(!is_valid_expansion<TestToAddressCall, int>::value);
+ static_assert(is_valid_expansion<TestToAddressCall, IntPtr>::value);
+ static_assert(is_valid_expansion<TestToAddressCall, TemplatedPtr<bool, true>>::value);
+
+ return true;
+}
+
+int main(int, char**) {
+ static_assert(test());
+ return 0;
+}
More information about the libcxx-commits
mailing list