Skip to content

Commit af718aa

Browse files
committed
path : C++26対応として表示関係の文字列取得関数を追加 (close #1517)
1 parent 69aa1ac commit af718aa

8 files changed

Lines changed: 346 additions & 7 deletions

File tree

lang/cpp26.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
297297
- `native_handle()`
298298
- [`std::filesystem::path`](/reference/filesystem/path.md)クラスに、文字列フォーマットのサポートを追加
299299
- [`std::filesystem::path`](/reference/filesystem/path.md)クラスに、出力用の文字列を取得するための、以下のメンバ関数を追加
300-
- [`display_string()`](/reference/filesystem/path/display_string.md.nolink)
301-
- [`system_encoded_string()`](/reference/filesystem/path/system_encoded_string.md.nolink)
302-
- [`generic_display_string()`](/reference/filesystem/path/generic_display_string.md.nolink)
303-
- [`generic_system_encoded_string()`](/reference/filesystem/path/generic_system_encoded_string.md.nolink)
300+
- [`display_string()`](/reference/filesystem/path/display_string.md)
301+
- [`system_encoded_string()`](/reference/filesystem/path/system_encoded_string.md)
302+
- [`generic_display_string()`](/reference/filesystem/path/generic_display_string.md)
303+
- [`generic_system_encoded_string()`](/reference/filesystem/path/generic_system_encoded_string.md)
304304

305305

306306
### 入出力
@@ -543,9 +543,12 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
543543
- [`<type_traits>`](/reference/type_traits.md)[`std::is_trivial`](/reference/type_traits/is_trivial.md)を非推奨化
544544
- これは[`std::is_trivially_copyable`](/reference/type_traits/is_trivially_copyable.md)[`std::is_trivially_default_constructible`](/reference/type_traits/is_trivially_default_constructible.md)の2つが合わさったものであるが、それらは異なる状況で必要になるものであった
545545
- [`std::memory_order::consume`](/reference/atomic/memory_order.md)と、それに関連して[`std::kill_dependency()`](/reference/atomic/kill_dependency.md)を非推奨化し、[`[[carries_dependency]]`](/lang/cpp11/attributes.md)属性を削除
546-
- [`std::filesystem::path`](/reference/filesystem/path.md)クラスの以下のメンバ関数を非推奨化
546+
- [`std::filesystem::path`](/reference/filesystem/path.md)クラスの、以下の非テンプレート版メンバ関数を非推奨化
547547
- [`string()`](/reference/filesystem/path/string.md)
548548
- [`generic_string()`](/reference/filesystem/path/generic_string.md)
549+
- これらはOS依存のパス名エンコーディングへ変換するため、[iostream](/reference/iostream.md)[`std::format()`](/reference/format/format.md)[`std::print()`](/reference/print/print.md)を含むほぼすべての標準のテキスト処理・入出力機能と互換性がなく、文字化けやデータ損失の原因になりやすい
550+
- レガシーなシステムAPIにパスを渡す用途では、システム依存エンコーディングへの変換であることを明確にした[`system_encoded_string()`](/reference/filesystem/path/system_encoded_string.md)[`generic_system_encoded_string()`](/reference/filesystem/path/generic_system_encoded_string.md)を使用する
551+
- 表示・フォーマットの用途では、[`display_string()`](/reference/filesystem/path/display_string.md)[`generic_display_string()`](/reference/filesystem/path/generic_display_string.md)、もしくは[`std::format()`](/reference/format/format.md)[`std::print()`](/reference/print/print.md)を使用する
549552

550553

551554
### 非推奨の取り消し

reference/filesystem/path.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ namespace std::filesystem {
6060
| [`native`](path/native.md) | システムの文字コードとしてパス文字列を取得する | C++17 |
6161
| [`c_str`](path/c_str.md) | システムの文字コードとしてC言語の文字列表現を取得する | C++17 |
6262
| [`operator string_type`](path/op_string_type.md) | システムの文字コードとして文字列型に変換する | C++17 |
63-
| [`string`](path/string.md) | システムのマルチバイト文字コードとしてパス文字列を取得する | C++17 |
63+
| [`string`](path/string.md) | システムのマルチバイト文字コードとしてパス文字列を取得する | C++17<br/> 非テンプレート版はC++26で非推奨 |
64+
| [`display_string`](path/display_string.md) | 表示用のリテラルエンコーディングとしてパス文字列を取得する | C++26 |
65+
| [`system_encoded_string`](path/system_encoded_string.md) | システム依存エンコーディングとしてパス文字列を取得する | C++26 |
6466
| [`wstring`](path/wstring.md) | システムのワイド文字コードとしてパス文字列を取得する | C++17 |
6567
| [`u8string`](path/u8string.md) | UTF-8エンコードとしてパス文字列を取得する | C++17 |
6668
| [`u16string`](path/u16string.md) | UTF-16エンコードでパス文字列を取得する | C++17 |
@@ -70,7 +72,9 @@ namespace std::filesystem {
7072
7173
| 名前 | 説明 | 対応バージョン |
7274
|------|------|----------------|
73-
| [`generic_string`](path/generic_string.md) | システムのマルチバイト文字コードとして、環境非依存パスフォーマットのパス文字列を取得する | C++17 |
75+
| [`generic_string`](path/generic_string.md) | システムのマルチバイト文字コードとして、環境非依存パスフォーマットのパス文字列を取得する | C++17<br/> 非テンプレート版はC++26で非推奨 |
76+
| [`generic_display_string`](path/generic_display_string.md) | 表示用のリテラルエンコーディングとして、環境非依存パスフォーマットのパス文字列を取得する | C++26 |
77+
| [`generic_system_encoded_string`](path/generic_system_encoded_string.md) | システム依存エンコーディングとして、環境非依存パスフォーマットのパス文字列を取得する | C++26 |
7478
| [`generic_wstring`](path/generic_wstring.md) | システムのワイド文字コードとして、環境非依存パスフォーマットのパス文字列を取得する | C++17 |
7579
| [`generic_u8string`](path/generic_u8string.md) | UTF-8エンコードで、環境非依存パスフォーマットのパス文字列を取得する | C++17 |
7680
| [`generic_u16string`](path/generic_u16string.md) | UTF-16エンコードで、環境非依存パスフォーマットのパス文字列を取得する | C++17 |
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# display_string
2+
* filesystem[meta header]
3+
* std::filesystem[meta namespace]
4+
* path[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
std::string display_string() const;
10+
```
11+
12+
## 概要
13+
表示に適したリテラルエンコーディングで、パス文字列を取得する。
14+
15+
[`std::format()`](/reference/format/format.md)[`std::print()`](/reference/print/print.md)で出力するのに適した文字列を返す。
16+
17+
18+
## 戻り値
19+
以下と等価である:
20+
21+
```cpp
22+
return std::format("{}", *this);
23+
```
24+
* std::format[link /reference/format/format.md]
25+
26+
27+
## 備考
28+
- 戻り値の文字列は、フォーマット ([`std::format()`](/reference/format/format.md)) や出力 ([`std::print()`](/reference/print/print.md)) で使用するのに適している
29+
- リテラルエンコーディングがUTF-8であり、パスが妥当なUnicodeであれば (Windowsのほとんどのパスがそうである)、文字コード変換は無損失となる
30+
31+
32+
##
33+
```cpp example
34+
#include <print>
35+
#include <filesystem>
36+
37+
namespace fs = std::filesystem;
38+
39+
int main()
40+
{
41+
fs::path p = "/usr/bin/clang";
42+
43+
std::string s = p.display_string();
44+
std::print("{}\n", s);
45+
}
46+
```
47+
* p.display_string()[color ff0000]
48+
49+
### 出力
50+
```
51+
/usr/bin/clang
52+
```
53+
54+
55+
## バージョン
56+
### 言語
57+
- C++26
58+
59+
### 処理系
60+
- [Clang](/implementation.md#clang): 22 [mark noimpl]
61+
- [GCC](/implementation.md#gcc): 16.1 [mark noimpl]
62+
- [Visual C++](/implementation.md#visual_cpp): 2026 Update 2 [mark noimpl]
63+
64+
65+
## 関連項目
66+
- [`string()`](string.md) (システムのマルチバイト文字コードで取得する。C++26で非推奨)
67+
- [`system_encoded_string()`](system_encoded_string.md) (システム依存エンコーディングで取得する)
68+
- [`generic_display_string()`](generic_display_string.md) (環境非依存フォーマットで取得する)
69+
- [`formatter`](formatter.md) ([`std::format()`](/reference/format/format.md)での書式)
70+
71+
72+
## 参照
73+
- [P2319R5 Prevent path presentation problems](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2319r5.html)
74+
- 表示用のリテラルエンコーディングで文字列を取得するこの関数がC++26で追加された
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# generic_display_string
2+
* filesystem[meta header]
3+
* std::filesystem[meta namespace]
4+
* path[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
std::string generic_display_string() const;
10+
```
11+
12+
## 概要
13+
表示に適したリテラルエンコーディングで、環境非依存パスフォーマットのパス文字列を取得する。
14+
15+
[`std::format()`](/reference/format/format.md)[`std::print()`](/reference/print/print.md)で出力するのに適した文字列を返す。
16+
17+
18+
## 戻り値
19+
以下と等価である:
20+
21+
```cpp
22+
return std::format("{:g}", *this);
23+
```
24+
* std::format[link /reference/format/format.md]
25+
26+
27+
## 備考
28+
- 戻り値の文字列は、フォーマット ([`std::format()`](/reference/format/format.md)) や出力 ([`std::print()`](/reference/print/print.md)) で使用するのに適している
29+
- リテラルエンコーディングがUTF-8であり、パスが妥当なUnicodeであれば (Windowsのほとんどのパスがそうである)、文字コード変換は無損失となる
30+
31+
32+
##
33+
```cpp example
34+
#include <print>
35+
#include <filesystem>
36+
37+
namespace fs = std::filesystem;
38+
39+
int main()
40+
{
41+
fs::path p = "foo\\bar"; // ネイティブフォーマットのパス (ディレクトリ区切り文字がバックスラッシュ)
42+
43+
std::string s = p.generic_display_string();
44+
std::print("{}\n", s);
45+
}
46+
```
47+
* p.generic_display_string()[color ff0000]
48+
49+
### 出力
50+
```
51+
foo/bar
52+
```
53+
54+
55+
## バージョン
56+
### 言語
57+
- C++26
58+
59+
### 処理系
60+
- [Clang](/implementation.md#clang): 22 [mark noimpl]
61+
- [GCC](/implementation.md#gcc): 16.1 [mark noimpl]
62+
- [Visual C++](/implementation.md#visual_cpp): 2026 Update 2 [mark noimpl]
63+
64+
65+
## 関連項目
66+
- [`generic_string()`](generic_string.md) (システムのマルチバイト文字コードで取得する。C++26で非推奨)
67+
- [`generic_system_encoded_string()`](generic_system_encoded_string.md) (システム依存エンコーディングで取得する)
68+
- [`display_string()`](display_string.md) (システムフォーマットで取得する)
69+
- [`formatter`](formatter.md) ([`std::format()`](/reference/format/format.md)での書式)
70+
71+
72+
## 参照
73+
- [P2319R5 Prevent path presentation problems](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2319r5.html)
74+
- 表示用のリテラルエンコーディングで環境非依存フォーマットの文字列を取得するこの関数がC++26で追加された

reference/filesystem/path/generic_string.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* path[meta class]
55
* function[meta id-type]
66
* cpp17[meta cpp]
7+
* cpp26deprecated[meta cpp]
78

89
```cpp
910
template <class EcharT,
@@ -15,6 +16,9 @@ std::basic_string<EcharT, traits, Allocator>
1516
std::string generic_string() const; // (2)
1617
```
1718
19+
(2)は、C++26で非推奨となった。システム依存エンコーディングへの変換であることを明確にした[`generic_system_encoded_string()`](generic_system_encoded_string.md)、もしくは表示に適した[`generic_display_string()`](generic_display_string.md)を使用すること。
20+
21+
1822
## 概要
1923
- (1) : 指定された文字型に対応する文字コードで、環境非依存パスフォーマットのパス文字列を取得する
2024
- (2) : システムのマルチバイト文字コードで、環境非依存パスフォーマットのパス文字列を取得する
@@ -37,6 +41,15 @@ std::string generic_string() const; // (2)
3741
- POSIXベースシステムではUTF-8、Windowsの日本語環境ではCP932文字コードとなる
3842
3943
44+
## 非推奨の詳細 (C++26)
45+
(2)が返すシステム依存のパス名エンコーディングは、[iostream](/reference/iostream.md)・[`std::format()`](/reference/format/format.md)・[`std::print()`](/reference/print/print.md)を含むほぼすべての標準のテキスト処理・入出力機能と互換性がなく、文字化けやデータ損失の原因になりやすい。C++26ではこの問題を避けるために(2)が非推奨となり、用途に応じて以下の代替関数が追加された:
46+
47+
- レガシーなシステムAPIにパスを渡す場合 : [`generic_system_encoded_string()`](generic_system_encoded_string.md) ((2)と同じ動作)
48+
- 表示・フォーマットする場合 : [`generic_display_string()`](generic_display_string.md)、[`std::format()`](/reference/format/format.md)、[`std::print()`](/reference/print/print.md)
49+
50+
なお、テンプレート版である(1)は非推奨となっていない。
51+
52+
4053
## 例
4154
### POSIXベースシステムでの例
4255
```cpp example
@@ -121,3 +134,13 @@ foo/bar
121134
- [Clang](/implementation.md#clang):
122135
- [GCC](/implementation.md#gcc): 8.1 [mark verified]
123136
- [Visual C++](/implementation.md#visual_cpp): 2017 Update 7 [mark verified]
137+
138+
139+
## 関連項目
140+
- [`generic_system_encoded_string()`](generic_system_encoded_string.md) (システム依存エンコーディングで取得する。(2)の代替)
141+
- [`generic_display_string()`](generic_display_string.md) (表示用のリテラルエンコーディングで取得する)
142+
143+
144+
## 参照
145+
- [P2319R5 Prevent path presentation problems](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2319r5.html)
146+
- 非テンプレート版の(2)がC++26で非推奨となり、[`generic_system_encoded_string()`](generic_system_encoded_string.md)[`generic_display_string()`](generic_display_string.md)が代替として追加された
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# generic_system_encoded_string
2+
* filesystem[meta header]
3+
* std::filesystem[meta namespace]
4+
* path[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
std::string generic_system_encoded_string() const;
10+
```
11+
12+
## 概要
13+
OS依存のパス名エンコーディング (システムのマルチバイト文字コード) で、環境非依存パスフォーマットのパス文字列を取得する。
14+
15+
16+
## 戻り値
17+
環境非依存フォーマットのパス文字列を返す。
18+
19+
文字コード変換が必要な場合、その変換は[`<filesystem>`](/reference/filesystem.md)のパス変換規則に従って行われる。
20+
21+
22+
## 備考
23+
- 文字コード変換は損失をともなう可能性があるため、この関数で取得した文字列はレガシーなシステムAPIにパスを渡す用途にのみ適している
24+
- 表示やフォーマットの用途では、[`generic_display_string()`](generic_display_string.md)[`std::format()`](/reference/format/format.md)[`std::print()`](/reference/print/print.md)を使用すること
25+
26+
27+
##
28+
```cpp example
29+
#include <iostream>
30+
#include <filesystem>
31+
32+
namespace fs = std::filesystem;
33+
34+
int main()
35+
{
36+
fs::path p = "foo\\bar"; // ネイティブフォーマットのパス (ディレクトリ区切り文字がバックスラッシュ)
37+
38+
std::string s = p.generic_system_encoded_string();
39+
std::cout << s << std::endl;
40+
}
41+
```
42+
* p.generic_system_encoded_string()[color ff0000]
43+
44+
### 出力
45+
```
46+
foo/bar
47+
```
48+
49+
50+
## バージョン
51+
### 言語
52+
- C++26
53+
54+
### 処理系
55+
- [Clang](/implementation.md#clang): 22 [mark noimpl]
56+
- [GCC](/implementation.md#gcc): 16.1 [mark noimpl]
57+
- [Visual C++](/implementation.md#visual_cpp): 2026 Update 2 [mark noimpl]
58+
59+
60+
## 関連項目
61+
- [`generic_string()`](generic_string.md) (この関数と同等の動作だが、C++26で非推奨)
62+
- [`generic_display_string()`](generic_display_string.md) (表示用のリテラルエンコーディングで取得する)
63+
- [`system_encoded_string()`](system_encoded_string.md) (システムフォーマットで取得する)
64+
65+
66+
## 参照
67+
- [P2319R5 Prevent path presentation problems](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2319r5.html)
68+
- 非推奨となった[`generic_string()`](generic_string.md)に代わり、システム依存エンコーディングへの変換であることを明確にしたこの関数がC++26で追加された

reference/filesystem/path/string.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* path[meta class]
55
* function[meta id-type]
66
* cpp17[meta cpp]
7+
* cpp26deprecated[meta cpp]
78

89
```cpp
910
template <class EcharT,
@@ -15,6 +16,9 @@ std::basic_string<EcharT, traits, Allocator>
1516
std::string string() const; // (2)
1617
```
1718
19+
(2)は、C++26で非推奨となった。システム依存エンコーディングへの変換であることを明確にした[`system_encoded_string()`](system_encoded_string.md)、もしくは表示に適した[`display_string()`](display_string.md)を使用すること。
20+
21+
1822
## 概要
1923
- (1) : 指定された文字型に対応する文字コードで、パス文字列を取得する
2024
- (2) : システムのマルチバイト文字コードで、パス文字列を取得する
@@ -37,6 +41,15 @@ std::string string() const; // (2)
3741
- POSIXベースシステムではUTF-8、Windowsの日本語環境ではCP932文字コードとなる
3842
3943
44+
## 非推奨の詳細 (C++26)
45+
(2)が返すシステム依存のパス名エンコーディングは、[iostream](/reference/iostream.md)・[`std::format()`](/reference/format/format.md)・[`std::print()`](/reference/print/print.md)を含むほぼすべての標準のテキスト処理・入出力機能と互換性がなく、文字化けやデータ損失の原因になりやすい。C++26ではこの問題を避けるために(2)が非推奨となり、用途に応じて以下の代替関数が追加された:
46+
47+
- レガシーなシステムAPIにパスを渡す場合 : [`system_encoded_string()`](system_encoded_string.md) ((2)と同じ動作)
48+
- 表示・フォーマットする場合 : [`display_string()`](display_string.md)、[`std::format()`](/reference/format/format.md)、[`std::print()`](/reference/print/print.md)
49+
50+
なお、テンプレート版である(1)は非推奨となっていない。
51+
52+
4053
## 例
4154
### POSIXベースシステムでの例
4255
```cpp example
@@ -121,3 +134,14 @@ foo/bar
121134
- [Clang](/implementation.md#clang):
122135
- [GCC](/implementation.md#gcc): 8.1 [mark verified]
123136
- [Visual C++](/implementation.md#visual_cpp): 2017 Update 7 [mark verified]
137+
138+
139+
## 関連項目
140+
- [`system_encoded_string()`](system_encoded_string.md) (システム依存エンコーディングで取得する。(2)の代替)
141+
- [`display_string()`](display_string.md) (表示用のリテラルエンコーディングで取得する)
142+
- [`native()`](native.md) (システムの文字コードでパス文字列を取得する)
143+
144+
145+
## 参照
146+
- [P2319R5 Prevent path presentation problems](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2319r5.html)
147+
- 非テンプレート版の(2)がC++26で非推奨となり、[`system_encoded_string()`](system_encoded_string.md)[`display_string()`](display_string.md)が代替として追加された

0 commit comments

Comments
 (0)