[libcxx-commits] [libcxx] [libc++][test] Fixes constexpr nasty_char_traits. (PR #90981)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue May 7 08:53:47 PDT 2024
================
@@ -16,6 +16,30 @@
#include "test_macros.h"
+// Tests whether the range [p1, p1 + n) overlaps with the range [p2, p2 + n).
+//
+// precondition The ranges [p1, p1 + n) and [p2, p2 + n) are valid ranges.
+//
+// Typically the pointers are compared with less than. This is not allowed when
+// the pointers belong to different ranges. This is UB. Typically, this is
+// benign at run-time, however since UB is not allowed during constant
+// evaluation this does not compile. This function does the validation without
+// UB.
+//
+// When the ranges overlap the ranges can be copied from the beginning to the
+// end. Otherwise they need to be copied from the end to the beginning.
+template <class Traits>
+TEST_CONSTEXPR_CXX14 bool is_overlapping_range(Traits* p1, const Traits* p2, std::size_t n) {
----------------
ldionne wrote:
As-is, this function does not catch the following case:
```
|-----------------------|
^ |---------------------|
| ^
p2 |
p1
```
I think the name of this function is misleading. Maybe the following API would be better instead?
```c++
template <class CharT>
bool is_pointer_in_range(CharT* first, CharT* last, CharT* p) {
// return whether p falls inside [first, last)
}
```
Then below you basically query for `is_pointer_in_range(p1, p1+n, p2)` which tells you whether `p1 <= p2 < p1+n`.
https://github.com/llvm/llvm-project/pull/90981
More information about the libcxx-commits
mailing list