命名空间
变体
操作

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

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

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

内容

[编辑] 格式规范

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

fill-and-align (可选) width (可选) ?(可选) g(可选)

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

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

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()。将 s 写入 ctx.out(),由 path-format-spec 指定。

对于路径名的字符转码

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

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

[编辑] 注意

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

[编辑] 示例

[编辑] 参见

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