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

履歴 編集

class
<filesystem>

std::formatter(C++26)

namespace std {
  template <class charT>
  struct formatter<filesystem::path, charT>;
}

概要

filesystem::pathクラスに対するstd::formatterクラステンプレートの特殊化。

書式としては、以下を使用できる:

[[fill] align] [width] [?] [g]

  • fill : アライメントに使う文字 (デフォルト: スペース)
  • align : アライメント(デフォルトは型による)
    • > : 右寄せ
    • < : 左寄せ
    • ^ : 中央寄せ
  • width : 幅 (アライメントもしくは0埋めの幅)
    • 置換フィールドを使って変数で指定できる
  • ? : デバッグ出力
    • 文字・文字列を引用符で囲み、エスケープシーケンスをエスケープして出力
  • g : 環境非依存のパスフォーマット (generic_string()) を使用する
    • これが指定されない場合は、プラットフォームごとのパスフォーマット (native()) を使用する

#include <print>
#include <filesystem>

namespace fs = std::filesystem;

int main() {
  fs::path a = "/a/b/c.txt";
  fs::path b = "multi\nline";
  fs::path c = "a\\b\\c.txt";

  // デフォルトフォーマットは、operator<<とちがって
  // 引用符で囲まずに出力する
  std::println("1 : {}", a);

  // デバッグ出力。
  // 引用符で囲み、エスケープシーケンスをエスケープして出力する
  std::println("2 : {:?}", a);
  std::println("3 : {:?}", b);

  // 環境非依存のパスフォーマット
  std::println("4 : {:g}", c);
  std::println("5 : {}", c);
}

出力例

1. /a/b/c.txt
2. "/a/b/c.txt"
3. "multi\nline"
4. a/b/c.txt
5. a\b\c.txt

バージョン

言語

  • C++26

処理系

関連項目

参照