[libcxx-commits] [libcxx] [libc++] Disallow std::prev(non_cpp17_bidi_iterator) (PR #112102)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Oct 18 10:20:12 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:
The intent of my comment is that we could replace the runtime assertion above by:
```cpp
if constexpr (__builtin_constant_p(__n) && __n > 0) {
static_assert(__has_bidirectional_iterator_category<_InputIter>::value, "this makes no sense");
} else {
_LIBCPP_ASSERT_PEDANTIC(__n <= 0 || __has_bidirectional_iterator_category<_InputIter>::value);
}
```
Essentially, we're making sure to provide a compile-time error when we can. And using a separate overload is just one way of achieving that (although I guess the `__builtin_constant_p` approach is even better). One problem is that this doesn't work pre-C++17.
https://github.com/llvm/llvm-project/pull/112102
More information about the libcxx-commits
mailing list