Skip to content

Commit 78f2291

Browse files
committed
path: C++26対応としてformatterの特殊化を追加 (close #1336)
1 parent 1bd673d commit 78f2291

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

reference/filesystem/path.md

+6
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ namespace std::filesystem {
187187
|------|------|----------------|
188188
| [`u8path`](u8path.md) | UTF-8エンコードされた文字列からパスオブジェクトを構築する | C++17<br/> C++20で非推奨 |
189189
190+
## 文字列フォーマットサポート
191+
192+
| 名前 | 説明 | 対応バージョン |
193+
|------|------|----------------|
194+
| [`formatter`](path/formatter.md) | [`std::formatter`](/reference/format/formatter.md)クラスの特殊化 | C++26 |
195+
190196
191197
## 例
192198
### POSIXベースシステムの例
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# formatter
2+
* filesystem[meta header]
3+
* std[meta namespace]
4+
* class[meta id-type]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template <class charT>
10+
struct formatter<filesystem::path, charT>;
11+
}
12+
```
13+
14+
## 概要
15+
`filesystem::path`クラスに対する[`std::formatter`](/reference/format/formatter.md)クラステンプレートの特殊化。
16+
17+
書式としては、以下を使用できる:
18+
19+
```
20+
[[fill] align] [width] [?] [g]
21+
```
22+
23+
* `fill` : アライメントに使う文字 (デフォルト: スペース)
24+
* `align` : アライメント(デフォルトは型による)
25+
* `>` : 右寄せ
26+
* `<` : 左寄せ
27+
* `^` : 中央寄せ
28+
* `width` : 幅 (アライメントもしくは0埋めの幅)
29+
* 置換フィールドを使って変数で指定できる
30+
* `?` : デバッグ出力
31+
* 文字・文字列を引用符で囲み、エスケープシーケンスをエスケープして出力
32+
* `g` : 環境非依存のパスフォーマット ([`generic_string()`](generic_string.md)) を使用する
33+
* これが指定されない場合は、プラットフォームごとのパスフォーマット ([`native()`](native.md)) を使用する
34+
35+
36+
##
37+
```cpp example
38+
#include <print>
39+
#include <filesystem>
40+
41+
namespace fs = std::filesystem;
42+
43+
int main() {
44+
fs::path a = "/a/b/c.txt";
45+
fs::path b = "multi\nline";
46+
fs::path c = "a\\b\\c.txt";
47+
48+
// デフォルトフォーマットは、operator<<とちがって
49+
// 引用符で囲まずに出力する
50+
std::println("1 : {}", a);
51+
52+
// デバッグ出力。
53+
// 引用符で囲み、エスケープシーケンスをエスケープして出力する
54+
std::println("2 : {:?}", a);
55+
std::println("3 : {:?}", b);
56+
57+
// 環境非依存のパスフォーマット
58+
std::println("4 : {:g}", c);
59+
std::println("5 : {}", c);
60+
}
61+
```
62+
63+
### 出力例
64+
```
65+
1. /a/b/c.txt
66+
2. "/a/b/c.txt"
67+
3. "multi\nline"
68+
4. a/b/c.txt
69+
5. a\b\c.txt
70+
```
71+
72+
## バージョン
73+
### 言語
74+
- C++26
75+
76+
### 処理系
77+
- [Clang](/implementation.md#clang): 20.0 [mark noimpl]
78+
- [GCC](/implementation.md#gcc): 14 [mark noimpl]
79+
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 10 [mark noimpl]
80+
81+
82+
## 関連項目
83+
- [`std::format()`](/reference/format/format.md) (フォーマットの詳細)
84+
85+
86+
## 参照
87+
- [P2845R8 Formatting of `std::filesystem::path`](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2845r8.html)

0 commit comments

Comments
 (0)