std::formatter<std::filesystem::path>
來自 cppreference.com
< 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 |
||
令 s 為 p.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<CharT, char> 為 true,
- std::is_same_v<typename path::value_type, wchar_t> 為 true,且
- 普通字面量編碼是 UTF-8。
- 否則,如果 std::is_same_v<typename path::value_type, CharT> 為 true,則不執行轉碼。
- 否則,轉碼行為是實現定義的。
返回一個迭代器,指向輸出範圍的末尾之後。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_format_path |
202403L |
(C++26) | 對 std::filesystem::path 的格式化支援 |
[編輯] 示例
本節不完整 原因:無示例 |
[編輯] 參見
(C++20) |
定義給定型別的格式化規則 (類模板) |