• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    class
    <iterator>

    std::unreachable_sentinel_t

    namespace std {
      struct unreachable_sentinel_t {
        template<weakly_incrementable I>
        friend constexpr bool operator==(unreachable_sentinel_t, const I&) noexcept
        { return false; }
      };
    
      inline constexpr unreachable_sentinel_t unreachable_sentinel{};
    }
    

    概要

    unreachable_sentinel_tは、weakly_incrementableな型(イテレータ型)とともに用いて、任意の(半)開区間の上界を示すことのできる番兵型である。

    検索などを目的として範囲を走査する際、検索対象が範囲内に存在している場合その終端まで到達することは無く、範囲の探索は必ずしも範囲の先頭から行われる訳でも無い。任意の(半)開区間とは、ある範囲の内部にあるその様な走査の対象となる部分範囲のことを指す。unreachable_sentinelはその様な区間の終端を暗に指定する、その様な開区間の上界を表す番兵である。

    検索など、ある範囲の一部を走査する際にそのイテレータに対する番兵として用いる事で、範囲の終端チェックを省略して走査を効率化できる可能性がある。ただし、実際の終端に到達しても走査が終了しないため、利用にあたっては事前条件(終端となる値が存在しているか)が必ず満たされるかに注意を払う必要がある。

    非メンバ(Hidden friends)関数

    名前 説明 対応バージョン
    operator== 等値比較、常にfalseを返す C++20
    operator!= 非等値比較、常にtrueを返す (==により使用可能) C++20

    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <iostream>
    
    int main() {
      std::string str = "(any unbounded interval)";
    
      // 探したい値(ここでは'i')が範囲内に含まれていることが予め分かる場合に利用できる
      // 最適化によって範囲の終端チェックが省略され、効率的に探索できる可能性がある
      // もしその仮定が成立しない場合、イテレータによる走査は外側の範囲を飛び出し未定義動作を引き起こすため注意が必要である
      auto pos = std::ranges::find(str.begin(), std::unreachable_sentinel, 'i');
    
      // 'j'は元の文字列に含まれないため未定義動作を引き起こす
      //auto pos = std::ranges::find(str.begin(), std::unreachable_sentinel, 'j');
    
      std::cout << *pos;
    }
    

    出力

    i
    

    バージョン

    言語

    • C++20

    処理系

    参照