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::basic_format_parse_context::begin - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. format
  3. basic_format_parse_context
  4. begin

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

履歴 編集

function
<format>

std::basic_format_parse_context::begin(C++20)

constexpr const_iterator begin() const noexcept;

概要

残りの書式文字列の先頭を指すイテレータを取得する。

効果

メンバ変数として保持している、書式文字列の先頭を指すイテレータを返す。

備考

コンテキストがフォーマッターのparseメンバ関数に渡された時点で、この関数の値は以下のいずれかである。

  • 置換フィールドのオプション{:options...}がある場合、:の次の文字を指すイテレータ
  • オプションがない場合、 end() と等しいか、}を指すイテレータ

例

基本的な使い方

#include <iostream>
#include <format>

enum color { red, green, blue };

const char* color_names[] = { "red", "green", "blue" };

template<>
class std::formatter<color> {
  bool enable_quote = false;
public:
  constexpr auto parse(std::format_parse_context& ctx) {
    auto it = ctx.begin();
    if (it == ctx.end()) {
      return it;
    }

    if (*it == '%') {
      enable_quote = true;
      ++it;
    }
    return it;
  }

  auto format(color c, std::format_context& ctx) const {
    if (enable_quote) {
      return std::format_to(ctx.out(), "\"{}\"", color_names[c]);
    }
    else {
      return std::format_to(ctx.out(), "{}", color_names[c]);
    }
  }
};

int main()
{
  std::cout << std::format("{:%}", red) << std::endl;
}

出力

"red"

バージョン

言語

  • C++20

処理系

  • Clang: 19.1.6 ✅
  • GCC: 13 ✅
  • Visual C++: ??

参照

  • P0645R10 Text Formatting
You are visitor 01402520 — 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.