Cppreference has the following example of the usage of std::unreachable_sentinel
:
template<class CharT>
constexpr std::size_t strlen(const CharT* s)
{
return std::ranges::find(s, std::unreachable_sentinel, CharT{}) - s;
}
However, the standard states in [iterator.requirements.general]:
A sentinel s is called reachable from an iterator
i
if and only if there is a finite sequence of applications of the expression++i
that makesi == s
. If s is reachable fromi
, [i
,s
) denotes a valid range.… [skipping the paragraph about counted ranges, not applicable here] …
The result of the application of library functions to invalid ranges is undefined.
Does this really mean that the above code has undefined behaviour (even in cases where the input string is zero-terminated), and if so, in what context can std::unreachable_sentinel
be legally used then?