Skip to content

Commit 9ff68ed

Browse files
committed
basic_stringbuf : C++26 string_viewに対応 #1186
1 parent b30cc46 commit 9ff68ed

File tree

2 files changed

+156
-13
lines changed

2 files changed

+156
-13
lines changed

reference/sstream/basic_stringbuf/op_constructor.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,67 @@ basic_stringbuf(
5656

5757
basic_stringbuf(basic_stringbuf&& rhs); // (11) C++11
5858
basic_stringbuf(basic_stringbuf&& rhs, const Allocator& a); // (12) C++20
59+
60+
template<class T>
61+
explicit
62+
basic_stringbuf(
63+
const T& t,
64+
ios_base::openmode which = ios_base::in | ios_base::out); // (13) C++26
65+
66+
template<class T>
67+
basic_stringbuf(
68+
const T& t,
69+
const Allocator& a); // (14) C++26
70+
71+
template<class T>
72+
basic_stringbuf(
73+
const T& t,
74+
ios_base::openmode which,
75+
const Allocator& a); // (15) C++26
5976
```
6077
* ios_base[link /reference/ios/ios_base.md]
6178
* basic_string[link /reference/string/basic_string.md]
6279
6380
## 概要
6481
`basic_stringbuf`オブジェクトを構築する。
6582
83+
- (1) : デフォルトコンストラクタ
84+
- (2) : 指定されたモードで構築する
85+
- (3) : [`std::basic_string`](/reference/string/basic_string.md)オブジェクトのコピーと、指定されたモードで構築する
86+
- (4) : モードとアロケータを指定して構築する
87+
- (5) : アロケータを指定して構築する
88+
- (6) : [`std::basic_string`](/reference/string/basic_string.md)オブジェクトのコピーと、アロケータを指定して構築する
89+
- (7) : [`std::basic_string`](/reference/string/basic_string.md)オブジェクトのコピー、モード、アロケータを指定して構築する
90+
- (8) : `Allocator`に変換可能なアロケータ型をもつ`std::basic_string`オブジェクトのコピーと、指定されたモードで構築する
91+
- (9) : `Allocator`に変換可能なアロケータ型をもつ`std::basic_string`オブジェクトのコピーと、アロケータを指定して構築する
92+
- (10) : `Allocator`に変換可能なアロケータ型をもつ`std::basic_string`オブジェクトのコピー、モード、アロケータを指定して構築する
93+
- (11) : ムーブコンストラクタ
94+
- (12) : ムーブコンストラクタでアロケータを指定して構築する
95+
- (13) : [`basic_string_view`](/reference/string_view/basic_string_view.md)に変換可能な文字列を初期化し、モードを設定する
96+
- (14) : [`basic_string_view`](/reference/string_view/basic_string_view.md)に変換可能な文字列を初期化、アロケータを設定する
97+
- (15) : [`basic_string_view`](/reference/string_view/basic_string_view.md)に変換可能な文字列を初期化し、モードとアロケータを設定する
98+
99+
100+
## テンプレートパラメータ制約
101+
- (13), (14), (15) : `is_convertible_v<const T&, basic_string_view<CharT, Traits>>`が`true`であること
102+
103+
66104
## 効果
67105
- (1) : 内部の文字列バッファを空にし、モードは`ios_base::in | ios_base::out`に設定する
68106
- (2) : 内部の文字列バッファを空にし、モードは`which`に設定する
69107
- (3) : 内部の文字列バッファを`s`のコピー、モードは`which`に設定する
70108
- (4) : 内部の文字列バッファを`std::move(s)`で、モードは`which`に設定する
71109
- (5) : 内部の文字列バッファを空にし、モードは`which`、アロケータは`a`に設定する
72-
- (6), (7), (8), (9), (10), (11) : 各引数は内部状態の初期化に使用される
73-
- (12) : `rhs`から`basic_stringbuf`オブジェクトをムーブ構築する
74-
- (13) : `rhs`から`basic_stringbuf`オブジェクトをムーブ構築し、アロケータは`a`を使用する
110+
- (6), (7), (8), (9), (10) : 各引数は内部状態の初期化に使用される
111+
- (11) : `rhs`から`basic_stringbuf`オブジェクトをムーブ構築する
112+
- (12) : `rhs`から`basic_stringbuf`オブジェクトをムーブ構築し、アロケータは`a`を使用する
113+
- (13) : `basic_string_view<CharT, Traits>(t)`で文字列を初期化し、モードは`which`に設定する
114+
- (14) : `basic_string_view<CharT, Traits>(t)`で文字列を初期化し、モードは`ios_base::in | ios_base::out`、アロケータは`a`に設定する
115+
- (15) : `basic_string_view<CharT, Traits>(t)`で文字列を初期化し、モードは`which`、アロケータは`a`に設定する
75116
76117
77118
## 例
119+
### 基本的な使い方
78120
```cpp example
79121
#include <iostream>
80122
#include <sstream>
@@ -95,11 +137,37 @@ int main()
95137
* sputc[link /reference/streambuf/basic_streambuf/sputc.md]
96138
* str()[link str.md]
97139

98-
### 出力
140+
#### 出力
99141
```
100142
a
101143
initial!
102144
```
103145

146+
#### string_viewからの構築 (C++26)
147+
```cpp example
148+
#include <print>
149+
#include <sstream>
150+
#include <string_view>
151+
152+
int main()
153+
{
154+
std::string_view sv = "from string literal";
155+
std::stringbuf buf1{sv};
156+
std::println("{}", buf1.str());
157+
158+
std::string_view sv2 = "from string_view";
159+
std::stringbuf buf2{sv2};
160+
std::println("{}", buf2.str());
161+
}
162+
```
163+
164+
#### 出力
165+
```
166+
from string literal
167+
from string_view
168+
```
169+
104170
## 参照
105-
- [P0408R7 Efficient Access to `basic_stringbuf`'s Buffer](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0408r7.pdf)
171+
- [P0408R7 Efficient Access to `basic_stringbuf`'s Buffer](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0408r7.pdf)
172+
- [P2495R3 Interfacing stringstreams with `string_view`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2495r3.pdf)
173+
- C++26で[`std::string_view`](/reference/string_view/basic_string_view.md)に対応した

reference/sstream/basic_stringbuf/str.md

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,40 @@ template <class SAlloc>
1919
void str(const basic_string<CharT, Traits, SAlloc>& s); // (5) C++20
2020

2121
void str(const basic_string<CharT, Traits, Allocator>&& s); // (6) C++20
22+
23+
template<class T>
24+
void str(const T& t); // (7) C++26
2225
```
2326
* basic_string[link /reference/string/basic_string.md]
2427
2528
## 概要
2629
文字列オブジェクトを取得または設定する。
2730
31+
- (1) : 文字列オブジェクトを取得する
32+
- (2) : 文字列オブジェクトを取得し、`SAlloc`型のアロケータ`sa`によってメモリ確保する
33+
- (3) : 保持する文字列オブジェクトをムーブして取得する
34+
- (4) : 文字列オブジェクト`s`を設定する
35+
- (5) : `Allocator`に変換可能な`SAlloc`型のアロケータによって確保されているデータをコピーして、文字列オブジェクト`s`を設定する
36+
- (6) : 与えられた文字列オブジェクト`s`をムーブして設定する
37+
- (7) : [`basic_string_view`](/reference/string_view/basic_string_view.md)に変換可能な文字列を設定する
38+
39+
40+
## テンプレートパラメータ制約
41+
- (7) : `is_convertible_v<const T&, basic_string_view<CharT, Traits>>`が`true`であること
42+
43+
2844
## 効果
2945
- (1) : 文字列オブジェクトを取得する
3046
- (2) : 文字列オブジェクトを取得して、`SAlloc`型のアロケータ`sa`によって確保する
3147
- (3) : 保持する文字列オブジェクトをムーブして取得する
3248
- (4) : 文字列オブジェクト`s`を設定する
33-
- (5) : `SAlloc`型のアロケータによって確保されているデータをコピーして、文字列オブジェクト`s`を設定する
49+
- (5) : `Allocator`に変換可能な`SAlloc`型のアロケータによって確保されているデータをコピーして、文字列オブジェクト`s`を設定する
3450
- (6) : 与えられた文字列オブジェクト`s`をムーブして設定する
51+
- (7) : [`basic_string_view`](/reference/string_view/basic_string_view.md)`<CharT, Traits>(t)`から文字列を設定する
52+
3553
3654
## 例
55+
### 基本的な使い方
3756
```cpp example
3857
#include <iostream>
3958
#include <sstream>
@@ -64,26 +83,82 @@ int main()
6483
buf.sputc('Y');
6584
std::cout << "4: After writing: " << buf.str() << std::endl;
6685
67-
// C++20: ムーブによる設定
68-
std::string s = "Move test";
69-
buf.str(std::move(s));
70-
std::cout << "5: " << buf.str() << std::endl;
86+
// C++26: string_viewから設定
87+
// std::string_view sv = "string_view test";
88+
// buf.str(sv);
89+
// std::cout << "6: " << buf.str() << std::endl;
7190
7291
// 空文字列でクリア
7392
buf.str("");
74-
std::cout << "6: Empty? " << (buf.str().empty() ? "yes" : "no") << std::endl;
93+
std::cout << "7: Empty? " << (buf.str().empty() ? "yes" : "no") << std::endl;
7594
}
7695
```
7796
* sputn[link /reference/streambuf/basic_streambuf/sputn.md]
7897
* sbumpc[link /reference/streambuf/basic_streambuf/sbumpc.md]
7998
* sputc[link /reference/streambuf/basic_streambuf/sputc.md]
8099

81-
### 出力
100+
#### 出力
82101
```
83102
1: Hello, World!
84103
2: New content
85104
3: After reading 2 chars: Read/Write test
86105
4: After writing: ReXY/Write test
87106
5: Move test
88-
6: Empty? yes
107+
7: Empty? yes
89108
```
109+
110+
#### ムーブを使用する例 (C++20)
111+
```cpp example
112+
#include <iostream>
113+
#include <sstream>
114+
115+
int main() {
116+
std::stringbuf buf;
117+
118+
// ムーブして文字列を設定
119+
std::string s = "Move string";
120+
buf.str(std::move(s));
121+
std::cout << buf.str() << std::endl;
122+
123+
// ムーブで文字列を取得
124+
std::string r = std::move(buf).str();
125+
std::cout << r << std::endl;
126+
}
127+
```
128+
129+
#### 出力
130+
```
131+
Move string
132+
Move string
133+
```
134+
135+
### string_viewからの設定 (C++26)
136+
```cpp example
137+
#include <iostream>
138+
#include <sstream>
139+
#include <string_view>
140+
141+
int main() {
142+
std::stringbuf buf;
143+
144+
// 文字列リテラルを設定
145+
buf.str("set from string literal");
146+
std::cout << buf.str() << std::endl;
147+
148+
// string_viewを設定
149+
std::string_view sv = "set from string_view";
150+
buf.str(sv);
151+
std::cout << buf.str() << std::endl;
152+
}
153+
```
154+
155+
#### 出力
156+
```
157+
set from string literal
158+
set from string_view
159+
```
160+
161+
## 参照
162+
- [P0408R7 Efficient Access to `basic_stringbuf`'s Buffer](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0408r7.pdf)
163+
- [P2495R3 Interfacing stringstreams with `string_view`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2495r3.pdf)
164+
- C++26で[`std::string_view`](/reference/string_view/basic_string_view.md)に対応した

0 commit comments

Comments
 (0)