std::unreachable_sentinel_t, std::unreachable_sentinel
來自 cppreference.com
< cpp | 迭代器 (iterator)
定義於標頭檔案 <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
的常量。目錄 |
[編輯] 非成員函式
operator== (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 |
(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 (類模板) (定製點物件) |