Namespaces
Variants

std:: formatter <std::filesystem::path>

From cppreference.net
定义于头文件 <filesystem>
template < class CharT >
struct formatter < std:: filesystem :: path , CharT > ;
(C++26 起)

针对 std::formatter std::filesystem::path 类模板特化允许用户通过 格式化函数 将路径名转换为其文本表示形式。该特化是 调试启用 的。

目录

格式规范

格式规范 path-format-spec 的语法为:

填充与对齐  (可选) 宽度  (可选) ? (可选) g (可选)

fill-and-align width 的含义与 标准格式规范 中的定义相同。

? 选项用于将路径名格式化为 转义字符串

g 选项用于指定路径名采用 通用格式表示

成员函数

set_debug_format
启用将路径名格式化为 转义字符串 的功能
(公开成员函数)
parse
按照 path-format-spec 规范解析格式说明符
(公开成员函数)
format
按照 path-format-spec 规范写入格式化输出
(公开成员函数)

std::formatter<std::filesystem::path>:: set_debug_format

constexpr void set_debug_format ( ) ;

允许当前对象将路径名格式化为 转义字符串

std::formatter<std::filesystem::path>:: parse

constexpr auto parse ( std:: basic_format_parse_context < CharT > & ctx )
- > std:: basic_format_parse_context < CharT > :: iterator ;

将格式说明符解析为 路径格式规范 ,并将解析后的说明符存储在当前对象中。

返回指向 路径格式规范 末尾之后位置的迭代器。

std::formatter<std::filesystem::path>:: format

template < class FormatContext >

auto format ( const std:: filesystem :: path & p, FormatContext & ctx ) const

- > FormatContext :: iterator ;

s 为:若使用 g 选项则为 p. generic < std :: filesystem :: path :: value_type > ( ) ,否则为 p. native ( ) 。按照 path-format-spec 的规范将 s 写入 ctx. out ( )

关于路径名的字符转码:

  • 在以下情况下,路径名会从宽字符串的本机编码转码为 UTF-8,并将非法子序列的最大子部分替换为 U+FFFD 替换字符:
  • std:: is_same_v < typename path :: value_type , CharT > true ,则不执行转码。
  • 其他情况下,转码行为由实现定义。

返回指向输出范围末尾的迭代器。

注释

功能测试 标准 功能
__cpp_lib_format_path 202403L (C++26) std::filesystem::path 的格式化支持

示例

参见

(C++20)
为给定类型定义格式化规则
(类模板)