Skip to content

Commit 9f82d6e

Browse files
committed
C++26対応としてstringとstring_viewを連結する演算子オーバーロードを追加 #1338
1 parent 413e0cc commit 9f82d6e

3 files changed

Lines changed: 188 additions & 1 deletion

File tree

reference/string/basic_string/op_plus.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,40 @@ namespace std {
113113
constexpr basic_string<charT, traits, Allocator>
114114
operator+(basic_string<charT, traits, Allocator>&& lhs,
115115
charT rhs); // (12) C++20
116+
117+
template <class charT, class traits, class Allocator>
118+
constexpr basic_string<charT, traits, Allocator>
119+
operator+(const basic_string<charT, traits, Allocator>& lhs,
120+
type_identity_t<basic_string_view<charT, traits>> rhs); // (13) C++26
121+
122+
template <class charT, class traits, class Allocator>
123+
constexpr basic_string<charT, traits, Allocator>
124+
operator+(basic_string<charT, traits, Allocator>&& lhs,
125+
type_identity_t<basic_string_view<charT, traits>> rhs); // (14) C++26
116126
}
117127
```
118128
129+
* type_identity_t[link /reference/type_traits/type_identity.md]
130+
* basic_string_view[link /reference/string_view/basic_string_view.md]
131+
119132
## 概要
120133
`basic_string` オブジェクトの連結を行う。
121134
135+
- (1) : `basic_string`オブジェクト同士を連結する
136+
- (2) : 右辺値の`basic_string`オブジェクトと`basic_string`オブジェクトを連結する
137+
- (3) : `basic_string`オブジェクトと右辺値の`basic_string`オブジェクトを連結する
138+
- (4) : 右辺値の`basic_string`オブジェクト同士を連結する
139+
- (5) : 文字配列と`basic_string`オブジェクトを連結する
140+
- (6) : 文字配列と右辺値の`basic_string`オブジェクトを連結する
141+
- (7) : 文字と`basic_string`オブジェクトを連結する
142+
- (8) : 文字と右辺値の`basic_string`オブジェクトを連結する
143+
- (9) : `basic_string`オブジェクトと文字配列を連結する
144+
- (10) : 右辺値の`basic_string`オブジェクトと文字配列を連結する
145+
- (11) : `basic_string`オブジェクトと文字を連結する
146+
- (12) : 右辺値の`basic_string`オブジェクトと文字を連結する
147+
- (13) : `basic_string`オブジェクトと[`basic_string_view`](/reference/string_view/basic_string_view.md)オブジェクトを連結する
148+
- (14) : 右辺値の`basic_string`オブジェクトと[`basic_string_view`](/reference/string_view/basic_string_view.md)オブジェクトを連結する
149+
122150
123151
## 戻り値
124152
@@ -220,6 +248,21 @@ namespace std {
220248
```
221249
* push_back[link push_back.md]
222250
251+
- (13) : 以下と等価
252+
```cpp
253+
basic_string<charT, traits, Allocator> r = lhs;
254+
r.append(rhs);
255+
return r;
256+
```
257+
* append[link append.md]
258+
259+
- (14) : 以下と等価
260+
```cpp
261+
lhs.append(rhs);
262+
return std::move(lhs);
263+
```
264+
* append[link append.md]
265+
223266
224267
## 備考
225268
(5), (6) の形式の `lhs`、および、(9), (10) の形式の `rhs` の文字列長算出のために `traits::length()` が使用される
@@ -242,11 +285,14 @@ C++20からこの演算子による文字列連結時にアロケータがどの
242285
|(10) : `std::move(lhs) + "rhs"`|`lhs`|`lhs`|`lhs`|`lhs`|`lhs`|
243286
|(11) : `lhs + 'r'`|新規にデフォルト構築|`lhs`からのSOCCC|`lhs`|新規にデフォルト構築|`lhs`からのSOCCC|
244287
|(12) : `std::move(lhs) + 'r'`|`lhs`|`lhs`|`lhs`|`lhs`|`lhs`|
288+
|(13) : `lhs + sv`| | | | |`lhs`からのSOCCC|
289+
|(14) : `std::move(lhs) + sv`| | | | |`lhs`|
245290
246291
247292
表にあるように、C++17までの仕様に完全に準拠している実装は無かった上に各実装によって伝播仕様がバラバラだったため、この変更によって影響を受けるコードはほぼ無いと思われる。
248293
249294
## 例
295+
### 基本的な使い方
250296
```cpp example
251297
#include <iostream>
252298
#include <string>
@@ -263,11 +309,39 @@ int main()
263309
```
264310
* +[color ff0000]
265311

266-
### 出力
312+
#### 出力
267313
```
268314
Hello, world!
269315
```
270316

317+
### string_viewとの連結 (C++26)
318+
```cpp example
319+
#include <iostream>
320+
#include <string>
321+
#include <string_view>
322+
323+
int main()
324+
{
325+
std::string s = "Hello";
326+
std::string_view sv = ", World!";
327+
328+
// string + string_view (13)
329+
std::string result1 = s + sv;
330+
std::cout << result1 << '\n';
331+
332+
// string&& + string_view (14)
333+
std::string result2 = std::string("Hello") + sv;
334+
std::cout << result2 << '\n';
335+
}
336+
```
337+
* +[color ff0000]
338+
339+
#### 出力
340+
```
341+
Hello, World!
342+
Hello, World!
343+
```
344+
271345
## 関連項目
272346

273347
| 名前 | 説明 |
@@ -276,6 +350,19 @@ Hello, world!
276350
| [`push_back`](push_back.md) | 文字を追加する |
277351
| [`insert`](insert.md) | 文字/文字列を挿入する |
278352

353+
## バージョン
354+
### 言語
355+
- C++03
356+
- C++26 : (13), (14)
357+
358+
### 処理系
359+
- [Clang](/implementation.md#clang): ??
360+
- [GCC](/implementation.md#gcc): ??
361+
- [ICC](/implementation.md#icc): ??
362+
- [Visual C++](/implementation.md#visual_cpp): ??
363+
279364
## 参照
280365
- [P1165R1 Make stateful allocator propagation more consistent for operator+(basic_string)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1165r1.html)
281366
- [P0980R1 Making `std::string` constexpr](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0980r1.pdf)
367+
- [P2591R5 Concatenation of strings and string views](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2591r5.html)
368+
- C++26で`string_view`との連結が追加された

reference/string_view/basic_string_view.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ string_view hello = sv.substr(0, 5); // 先頭5文字を抽出する
140140

141141

142142
## 非メンバ関数
143+
### 文字列連結
144+
145+
| 名前 | 説明 | 対応バージョン |
146+
|------|------|----------------|
147+
| [`operator+`](basic_string_view/op_plus.md) | 文字列の連結 | C++26 |
148+
143149
### 比較演算
144150

145151
| 名前 | 説明 | 対応バージョン |
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# operator+
2+
* string[meta header]
3+
* std[meta namespace]
4+
* function template[meta id-type]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template <class charT, class traits, class Allocator>
10+
constexpr basic_string<charT, traits, Allocator>
11+
operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
12+
const basic_string<charT, traits, Allocator>& rhs); // (1) C++26
13+
14+
template <class charT, class traits, class Allocator>
15+
constexpr basic_string<charT, traits, Allocator>
16+
operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
17+
basic_string<charT, traits, Allocator>&& rhs); // (2) C++26
18+
}
19+
```
20+
* type_identity_t[link /reference/type_traits/type_identity.md]
21+
* basic_string[link /reference/string/basic_string.md]
22+
23+
## 概要
24+
[`basic_string_view`](../basic_string_view.md)オブジェクトと[`basic_string`](/reference/string/basic_string.md)オブジェクトを連結する。
25+
26+
- (1) : [`basic_string_view`](../basic_string_view.md)オブジェクトと`basic_string`オブジェクトを連結する
27+
- (2) : [`basic_string_view`](../basic_string_view.md)オブジェクトと右辺値の`basic_string`オブジェクトを連結する
28+
29+
`basic_string_view`側の引数に[`type_identity_t`](/reference/type_traits/type_identity.md)を使用することで非推論コンテキストとし、`basic_string_view`に暗黙変換可能な型からの連結を可能にしている。
30+
31+
32+
## 戻り値
33+
- (1) : 以下と等価
34+
```cpp
35+
basic_string<charT, traits, Allocator> r = rhs;
36+
r.insert(0, lhs);
37+
return r;
38+
```
39+
* insert[link /reference/string/basic_string/insert.md]
40+
41+
- (2) : 以下と等価
42+
```cpp
43+
rhs.insert(0, lhs);
44+
return std::move(rhs);
45+
```
46+
* insert[link /reference/string/basic_string/insert.md]
47+
48+
49+
## 例
50+
```cpp example
51+
#include <iostream>
52+
#include <string>
53+
#include <string_view>
54+
55+
int main()
56+
{
57+
std::string_view sv = "Hello, ";
58+
std::string s = "World!";
59+
60+
// string_view + string (1)
61+
std::string result1 = sv + s;
62+
std::cout << result1 << '\n';
63+
64+
// string_view + string&& (2)
65+
std::string result2 = sv + std::string("World!");
66+
std::cout << result2 << '\n';
67+
}
68+
```
69+
* +[color ff0000]
70+
71+
### 出力
72+
```
73+
Hello, World!
74+
Hello, World!
75+
```
76+
77+
## バージョン
78+
### 言語
79+
- C++26
80+
81+
### 処理系
82+
- [Clang](/implementation.md#clang): ??
83+
- [GCC](/implementation.md#gcc): ??
84+
- [ICC](/implementation.md#icc): ??
85+
- [Visual C++](/implementation.md#visual_cpp): ??
86+
87+
## 関連項目
88+
89+
| 名前 | 説明 |
90+
|-------------------------------|------------------------|
91+
| [`basic_string::operator+`](/reference/string/basic_string/op_plus.md) | 文字列の連結 |
92+
93+
## 参照
94+
- [P2591R5 Concatenation of strings and string views](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2591r5.html)

0 commit comments

Comments
 (0)