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

履歴 編集

function
<format>

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

constexpr const_iterator begin() const noexcept;

概要

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

効果

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

基本的な使い方

#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

処理系

参照