[libcxx-commits] [libcxx] [libc++] Disallow std::prev(non_cpp17_bidi_iterator) (PR #112102)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 15 06:11:31 PDT 2024


================
@@ -35,6 +39,13 @@ prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n =
   return __x;
 }
 
+template <class _BidirectionalIterator,
+          __enable_if_t<__has_bidirectional_iterator_category<_BidirectionalIterator>::value, int> = 0>
----------------
ldionne wrote:

About `enable_if` vs `static_assert`. Upon re-consideration, I think the right thing to do here would be a `static_assert`. Indeed, we have the runtime assertion `_LIBCPP_ASSERT_PEDANTIC(__n <= 0 || __has_bidirectional_iterator_category<_InputIter>::value)` above, and one way to think about this patch is that we're only "inlining" the knowledge that `n == 1` in this overload. That gives us:

```
_LIBCPP_ASSERT_PEDANTIC(false || __has_bidirectional_iterator_category<_InputIter>::value)
```

but then we might as well check that at compile-time since there is no runtime-dependent value anymore. So we get

```
static_assert(__has_bidirectional_iterator_category<_InputIter>::value)
```

for this overload.

That also means we should switch to a `.verify.cpp` test instead.

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


More information about the libcxx-commits mailing list