[libcxx-commits] [libcxx] [libcxx][test] std::array::iterator are not pointers by C++ standard (PR #70729)

Duo Wang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 1 13:34:52 PDT 2023


================
@@ -60,6 +60,13 @@ static_assert(!HasMakeHeapR<UncheckedRange<const int*>>); // Doesn't satisfy `so
 
 template <std::size_t N, class T, class Iter>
 constexpr void verify_heap(const std::array<T, N>& heapified, Iter last, std::array<T, N> expected) {
+  assert(heapified == expected);
----------------
wdunicornpro wrote:

For `last` in `verify_heap`, we get `contiguous_iterator<int*>` in some cases and `std::array::iterator` in some other cases, and we later on want to compare it with `heapified.end()`. Since there isn't a strictly conforming way to cast `std::array::iterator` to pointers (correct me if I'm wrong), the two solutions I was to either allow `contiguous_iterator` to contain `std::array::iterator`, or to check equality in different ways, as illustrated in [e69008d](https://github.com/llvm/llvm-project/pull/70729/commits/e69008dcd6965ecb723294136a26c343e7092b39#diff-3d1c9b09e86d6dace3fe63664d13c76632c4127dd690b9124f61c28af8130d03L61-L66).

In hindsight, the naming in [e69008d](https://github.com/llvm/llvm-project/pull/70729/commits/e69008dcd6965ecb723294136a26c343e7092b39#diff-3d1c9b09e86d6dace3fe63664d13c76632c4127dd690b9124f61c28af8130d03L61-L66) could be misleading. If the general idea of it looks good to you, I can refine it as below:
```cpp
template <std::size_t N, class T>
constexpr void verify_last(const std::array<T, N>& heapified, T *last) {
  assert(last == heapified.data() + heapified.size());
}

template <std::size_t N, class T>
constexpr void verify_last(const std::array<T, N>& heapified, const typename std::array<T, N>::iterator& last) {
  assert(last == heapified.end());
}

template <std::size_t N, class T, class Iter>
constexpr void verify_heap(const std::array<T, N>& heapified, Iter last, const std::array<T, N>& expected) {
  assert(heapified == expected);
  verify_last(heapified, base(last));
  assert(std::is_heap(heapified.begin(), heapified.end()));
}
```


https://github.com/llvm/llvm-project/pull/70729


More information about the libcxx-commits mailing list