名前空間
変種
操作

std::unreachable_sentinel_t, std::unreachable_sentinel

From cppreference.com
 
 
イテレータライブラリ
イテレータのコンセプト
イテレータのプリミティブ
アルゴリズムのコンセプトとユーティリティ
間接呼び出し可能コンセプト
共通アルゴリズム要件
(C++20)
(C++20)
(C++20)
ユーティリティ
(C++20)
イテレータアダプタ
unreachable_sentinel_tunreachable_sentinel
(C++20)(C++20)
Rangeアクセス
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
ヘッダ <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

{ 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
}

[編集] 関連項目

初期値を繰り返しインクリメントして生成されるシーケンスからなる view
(クラステンプレート) (カスタマイゼーションポイントオブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)