https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100990
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
So maybe like this, which is a partial fix for the general case, not just for
std::span:
--- a/libstdc++-v3/include/debug/helper_functions.h
+++ b/libstdc++-v3/include/debug/helper_functions.h
@@ -38,6 +38,10 @@
#include <bits/stl_pair.h> // for pair
+#ifdef __cpp_lib_concepts
+# include <bits/iterator_concepts.h>
+#endif
+
namespace __gnu_debug
{
template<typename _Iterator, typename _Sequence, typename _Category>
@@ -212,6 +216,25 @@ namespace __gnu_debug
std::__iterator_category(__first));
}
+#ifdef __cpp_lib_concepts
+ template<typename _InputIterator, std::sentinel_for<_InputIterator>
_Sentinel>
+ constexpr bool
+ __valid_range(_InputIterator __first, _Sentinel __last)
+ {
+ if constexpr (requires { typename std::common_type_t<_InputIterator,
+ _Sentinel>;
+ requires std::input_iterator<_Sentinel>;
+ })
+ {
+ using _CTp = std::common_type_t<_InputIterator, _Sentinel>;
+ return __gnu_debug::__valid_range(static_cast<_CTp>(__first),
+ static_cast<_CTp>(__last));
+ }
+ else
+ return true;
+ }
+#endif
+
#if __cplusplus >= 201103L
template<typename _InputIterator>
_GLIBCXX20_CONSTEXPR
This would mean there are no debug checks for most not-common ranges. The only
cases where we would still check anything would be like this testcase (which
repros the std::span problem):
#define _GLIBCXX_DEBUG
#include <span>
int main()
{
char buf[1];
const char* end = buf + 1;
std::span<char> s(buf, end);
}
A slightly more interesting case is where we have safe iterators from a debug
container:
#define _GLIBCXX_DEBUG
#include <span>
#include <vector>
int main()
{
std::vector<int> v;
std::span<int> s2(v.begin(), v.cbegin());
std::span<int> s3(v.begin(), std::vector<int>::const_iterator{});
}
Constructing s2 is OK, constructing s3 is not. With the patch above, we get a
debug mode failure at runtime because of the singular const_iterator{} object:
/home/jwakely/gcc/17/include/c++/17.0.0/debug/safe_iterator.h:1066:
In function:
constexpr gnu_debug::_Safe_iterator<gnu_cxx::normal_iterator<const int*,
std::vector<int, std::allocator<int> > >, std::debug::vector<int>,
std::random_access_iterator_tag>::difference_type
gnu_debug::operator-(const _Safe_iterator<gnu_cxx::normal_iterator<const
int*, std::vector<int, std::allocator<int> > >, std::debug::vector<int>,
std::random_access_iterator_tag>::_Self&, const _Safe_iterator<gnu_cxx::
normal_iterator<const int*, std::vector<int, std::allocator<int> > >,
std::debug::vector<int>, std::random_access_iterator_tag>::_OtherSelf&)
Error: attempt to compute the difference between a
singular (value-initialized) iterator to a past-the-end iterator.
Objects involved in the operation:
iterator "lhs" @ 0x7ffe82d0a250 {
type = gnu_cxx::normal_iterator<int const*, std::vector<int,
std::allocator<int> > > (constant iterator);
state = singular (value-initialized);
}
iterator "rhs" @ 0x7ffe82d0a280 {
type = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > > (mutable iterator);
state = past-the-end;
references sequence with type 'std::debug::vector<int,
std::allocator<int> >' @ 0x7ffe82d0a190
}
Aborted (core dumped) ./a.out