[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
This commit is contained in:
Arthur O'Dwyer 2021-09-21 16:35:16 -04:00
parent 35f7020098
commit 85d4e29fd8
3 changed files with 33 additions and 23 deletions

View File

@ -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>::difference_type difference_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

View File

@ -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;

View File

@ -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;
}