GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::incrementable - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. iterator
  3. incrementable

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

concept
<iterator>

std::incrementable(C++20)

namespace std {
  template<class I>
  concept incrementable =
    regular<I> &&
    weakly_incrementable<I> &&
    requires(I i) {
      { i++ } -> same_as<I>;
    };
}

概要

incrementableは、イテレータ型Iが前置/後置インクリメント演算子(operator++)によってインクリメント可能であり、コピー/ムーブ構築/代入とデフォルト構築、等値比較が可能であることを表すコンセプトである。

要件

このコンセプトによって要求されるインクリメント操作は全て等しさを保持しなければならない。これは、weakly_incrementableの注釈よりも優先される。

モデル

型Iのオブジェクトa, bについて次の条件を満たす場合に限って、型Iはincrementableのモデルである。

  • bool(a == b)がtrueならば、bool(a++ == b)
  • bool(a == b)がtrueならば、bool(((void)a++, a) == ++b)

a == bという要件は++a == ++bが等しいことを意味しており、これらの要件はイテレータにマルチパス保証を要求するものである。すなわち、あるイテレート範囲を複数のイテレータから同時に同じ順序で走査することができることを要求する。incrementableなイテレータはその操作によってイテレータが参照しているシーケンスの状態が変化してはならない。多くの一般的なイテレータがこの要件を満たしている。

例

#include <iostream>
#include <concepts>
#include <iterator>
#include <memory>
#include <vector>

template<std::incrementable I>
void f(const char* name) {
  std::cout << name << " is incrementable" << std::endl;
}

template<typename I>
void f(const char* name) {
  std::cout << name << " is not incrementable" << std::endl;
}


struct sample_incrementable {
  friend auto operator++(sample_incrementable&) -> sample_incrementable&;
  friend auto operator++(sample_incrementable&, int) -> sample_incrementable; // prvalueを返す必要がある

  friend bool operator==(const sample_incrementable&, const sample_incrementable&) = default;

  using difference_type = int;
};

struct sample_weak_incrementable {
  friend auto operator++(sample_weak_incrementable&) -> sample_weak_incrementable&;
  friend auto operator++(sample_weak_incrementable&, int) -> sample_weak_incrementable&;  

  using difference_type = int;
};


int main() {
  f<int*>("int*");
  f<const int*>("const int*");
  f<std::vector<int>::iterator>("std::vector<int>::iterator");
  f<sample_incrementable>("sample_incrementable");

  std::cout << "\n";
  f<int* const>("int* const");
  f<std::ostream_iterator<double>>("std::ostream_iterator<double>");
  f<sample_weak_incrementable>("sample_weak_incrementable");
}

出力

int* is incrementable
const int* is incrementable
std::vector<int>::iterator is incrementable
sample_incrementable is incrementable

int* const is not incrementable
std::ostream_iterator<double> is not incrementable
sample_weak_incrementable is not incrementable

バージョン

言語

  • C++20

処理系

  • Clang: 13.0.1 ✅
  • GCC: 10.1 ✅
  • Visual C++: 2019 Update 6 ✅

関連項目

  • C++20 コンセプト

参照

  • P0896R4 The One Ranges Proposal (was Merging the Ranges TS)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.