[libcxx-commits] [libcxx] 85d4e29 - [libc++] Fix __wrap_iter to be a proper contiguous iterator.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 22 15:52:10 PDT 2021


Author: Arthur O'Dwyer
Date: 2021-09-22T18:51:46-04:00
New Revision: 85d4e29fd84e1afe03a589e240e33aa5a5c4b7ce

URL: https://github.com/llvm/llvm-project/commit/85d4e29fd84e1afe03a589e240e33aa5a5c4b7ce
DIFF: https://github.com/llvm/llvm-project/commit/85d4e29fd84e1afe03a589e240e33aa5a5c4b7ce.diff

LOG: [libc++] Fix __wrap_iter to be a proper contiguous iterator.

Instead of overloading `__to_address`, let's specialize `pointer_traits`.
Function overloads need to be in scope at the point where they're called,
whereas template specializations do not. (User code can provide pointer_traits
specializations to be used by already-included library code, so obviously
`__wrap_iter` can do the same.)

`pointer_traits<__wrap_iter<It>>` cannot provide `pointer_to`, because
you generally cannot create a `__wrap_iter` without also knowing the
identity of the container into which you're trying to create an iterator.
I believe this is OK; contiguous iterators are required to provide
`to_address` but *not* necessarily `pointer_to`.

Differential Revision: https://reviews.llvm.org/D110198

Added: 
    

Modified: 
    libcxx/include/__iterator/wrap_iter.h
    libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
    libcxx/test/std/utilities/memory/pointer.traits/pointer_to.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__iterator/wrap_iter.h b/libcxx/include/__iterator/wrap_iter.h
index 9f869f4bf6070..0a978914b1aa9 100644
--- a/libcxx/include/__iterator/wrap_iter.h
+++ b/libcxx/include/__iterator/wrap_iter.h
@@ -13,7 +13,7 @@
 #include <__config>
 #include <__debug>
 #include <__iterator/iterator_traits.h>
-#include <__memory/pointer_traits.h> // __to_address
+#include <__memory/pointer_traits.h>
 #include <type_traits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -283,12 +283,18 @@ template <class _It>
 struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : true_type {};
 #endif
 
-template <class _Iter>
-_LIBCPP_CONSTEXPR
-decltype(_VSTD::__to_address(declval<_Iter>()))
-__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
-    return _VSTD::__to_address(__w.base());
-}
+template <class _It>
+struct _LIBCPP_TEMPLATE_VIS pointer_traits<__wrap_iter<_It> >
+{
+    typedef __wrap_iter<_It> pointer;
+    typedef typename pointer_traits<_It>::element_type element_type;
+    typedef typename pointer_traits<_It>::
diff erence_type 
diff erence_type;
+
+    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
+    static element_type *to_address(pointer __w) _NOEXCEPT {
+        return _VSTD::__to_address(__w.base());
+    }
+};
 
 _LIBCPP_END_NAMESPACE_STD
 

diff  --git a/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp b/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
index 45d034c5c232d..e0ec3b8d176eb 100644
--- a/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
+++ b/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp
@@ -10,10 +10,6 @@
 
 // UNSUPPORTED: c++03, c++11, c++14, c++17
 
-// TODO: We should enable this test in Debug mode once we fix __wrap_iter
-//       to be a proper contiguous_iterator.
-// UNSUPPORTED: LIBCXX-DEBUG-FIXME
-
 // template <class T> constexpr T* to_address(T* p) noexcept;
 // template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
 

diff  --git a/libcxx/test/std/utilities/memory/pointer.traits/pointer_to.pass.cpp b/libcxx/test/std/utilities/memory/pointer.traits/pointer_to.pass.cpp
index e9b858c7fd943..ebbbf8b973427 100644
--- a/libcxx/test/std/utilities/memory/pointer.traits/pointer_to.pass.cpp
+++ b/libcxx/test/std/utilities/memory/pointer.traits/pointer_to.pass.cpp
@@ -17,29 +17,37 @@
 
 #include <memory>
 #include <cassert>
+
 #include "test_macros.h"
 
-#if TEST_STD_VER > 17
-constexpr
-#endif
-bool check() {
+TEST_CONSTEXPR_CXX20 bool test()
+{
     {
         int i = 0;
-        static_assert((std::is_same<int *, decltype(std::pointer_traits<int*>::pointer_to(i))>::value), "");
-        int* a = std::pointer_traits<int*>::pointer_to(i);
-        assert(a == &i);
+        static_assert(std::is_same<decltype(std::pointer_traits<int*>::pointer_to(i)), int*>::value, "");
+        assert(std::pointer_traits<int*>::pointer_to(i) == &i);
     }
     {
-        (std::pointer_traits<void*>::element_type)0;
+        int i = 0;
+        static_assert(std::is_same<decltype(std::pointer_traits<const int*>::pointer_to(i)), const int*>::value, "");
+        assert(std::pointer_traits<const int*>::pointer_to(i) == &i);
     }
     return true;
 }
 
-int main(int, char**) {
-    check();
+int main(int, char**)
+{
+    test();
 #if TEST_STD_VER > 17
-    static_assert(check(), "");
+    static_assert(test());
 #endif
 
-  return 0;
+    {
+        // Check that pointer_traits<void*> is still well-formed, even though it has no pointer_to.
+        static_assert(std::is_same<std::pointer_traits<void*>::element_type, void>::value, "");
+        static_assert(std::is_same<std::pointer_traits<const void*>::element_type, const void>::value, "");
+        static_assert(std::is_same<std::pointer_traits<volatile void*>::element_type, volatile void>::value, "");
+    }
+
+    return 0;
 }


        


More information about the libcxx-commits mailing list