std::unreachable_sentinel_t, std::unreachable_sentinel
Определён в заголовке <iterator> | ||
|---|---|---|
struct unreachable_sentinel_t; | (1) | (начиная с C++20) |
inline constexpr unreachable_sentinel_t unreachable_sentinel{};
| (2) | (начиная с C++20) |
1)
unreachable_sentinel_t — это пустой тип класса, который можно использовать для обозначения «верхней границы» неограниченного интервала.
2)
unreachable_sentinel — это константа типа unreachable_sentinel_t.Нечлен-функции
|
(C++20) | сравнивает unreachable_sentinel_t со значением любого типа weakly_incrementable (шаблон функции) |
operator==(std::unreachable_sentinel_t)
template<std::weakly_incrementable I>
friend constexpr bool operator==( unreachable_sentinel_t, const I& ) noexcept
{ return false; }
| (начиная с C++20) |
unreachable_sentinel_t можно сравнивать со значением любого типа weakly_incrementable, и результат всегда равен false.
Этот шаблон функции не виден при обычном неквалифицированном или квалифицированном поиске и может быть найден только с помощью поиска, зависящего от аргументов, если std::unreachable_sentinel_t является ассоциированным классом аргументов.
Пример
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>
template<class CharT>
constexpr std::size_t strlen(const CharT* s)
{
return std::ranges::find(s, std::unreachable_sentinel, CharT{}) - s;
}
template<class CharT>
constexpr std::size_t find_first(const CharT* haystack, const CharT* needle)
{
const char* needle_end = needle + strlen(needle);
// search(begin, unreachable_sentinel) is usually more efficient than
// search(begin, end) due to one less comparison per cycle.
// But "needle" MUST BE PRESENT in the "haystack", otherwise the call
// is UB (which is a compile-time error in constexpr context).
auto found = std::ranges::search(haystack, std::unreachable_sentinel,
needle, needle_end);
return found.begin() - haystack;
}
int main()
{
static_assert(strlen("The quick brown fox jumps over a lazy dog.") == 42);
static_assert(find_first("unsigned short int", "short") == 9);
// static_assert(find_first("long int", "float")); // compile-time error
}
См. также
|
(C++20) | view, состоящий из последовательности, полученной многократным инкрементированием начального значения(шаблон класса) (объект точки настройки) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/iterator/unreachable_sentinel_t