命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | filesystem‎ | path
 
 
 
 
定义于头文件 <filesystem>
template< class CharT >
struct formatter<std::filesystem::path, CharT>;
(C++26 起)

std::filesystem::path 类的 std::formatter 模板特化允许用户使用格式化函数将路径名转换为其文本表示。此特化是启用调试 (debug-enabled) 的。

目录

[编辑] 格式规定

格式规定 path-format-spec 的语法是

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

填充与对齐宽度 的含义与标准格式规定中的相同。

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

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;

将格式说明符解析为 path-format-spec 并将解析后的说明符存储在当前对象中。

返回一个指向 path-format-spec 结尾之后位置的迭代器。

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

template< class FormatContext >

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

    -> FormatContext::iterator;

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

关于路径名的字符转码:

  • 如果满足以下条件,路径名会从宽字符字符串的原生编码转码为 UTF-8,其中格式错误的子序列的最大子部分将被替换为 U+FFFD REPLACEMENT CHARACTER:
  • 否则,如果 std::is_same_v<typename path::value_type, CharT>true,则不执行转码。
  • 否则,转码行为是实现定义的。

返回一个迭代器,指向输出范围的末尾之后。

[编辑] 注意

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

[编辑] 示例

[编辑] 参见

(C++20)
定义给定类型的格式化规则
(类模板) [编辑]