std::unreachable_sentinel_t, std::unreachable_sentinel
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Iterator library
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)(C++23)(C++20)(C++20)
(deprecated in C++17)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++14)
(C++11)
(C++11)
(C++20)(C++20)
unreachable_sentinel_tunreachable_sentinel
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++23)
(C++23)
(C++23)
(C++23)
(C++23)
(C++11)(C++14)
(C++14)(C++14)
Defined in header
<iterator>
struct unreachable_sentinel_t;
(1)
(since C++20)
inline constexpr unreachable_sentinel_t unreachable_sentinel{};
(2)
(since C++20)
1)
unreachable_sentinel_t
is an empty class type that can be used to denote the "upper bound" of an unbounded interval.2)
unreachable_sentinel
is a constant of type unreachable_sentinel_t
.[edit] Non-member functions
operator==
(C++20)
unreachable_sentinel_t
with a value of any weakly_incrementable
type (function template)
operator==(std::unreachable_sentinel_t)
template< std::weakly_incrementable I >
(since C++20)
friend constexpr bool operator==( unreachable_sentinel_t, const I& ) noexcept
unreachable_sentinel_t
can be compared with any weakly_incrementable
type and the result is always false.
This function template is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::unreachable_sentinel_t
is an associated class of the arguments.
[edit] Example
Run this code
#include <concepts> #include <cstddef> #include <iterator> #include <ranges> #include <utility> namespace ranges = std::ranges; // never checks "iter != r.end()" template<ranges::random_access_range R> constexpr std::size_t trivial_strlen(R&& r) { auto iter = r.begin(); while (*iter != ranges::range_value_t <R>{}) ++iter; return iter - r.begin(); } template<ranges::random_access_range R> constexpr std::size_t my_strlen(R&& r) { if constexpr (std::same_as <ranges::sentinel_t <R>, std::unreachable_sentinel_t>) return trivial_strlen(std::forward <R>(r)); else return ranges::find (std::forward <R>(r), ranges::range_value_t <R>{}) - ranges::begin (r); } int main() { constexpr static char str[] = "The quick brown fox jumps over a lazy dog."; static_assert(my_strlen(str) == 42); // finds the length of the string faster, but UB if "str" is not null-terminated constexpr auto unsafe_str = ranges::subrange {str, std::unreachable_sentinel}; static_assert(my_strlen(unsafe_str) == 42); }