Skip to content

Commit e463c2f

Browse files
committed
algorithm/fill_n : C++26波カッコ初期化に対応 #1311
1 parent 319fdf9 commit e463c2f

File tree

2 files changed

+140
-14
lines changed

2 files changed

+140
-14
lines changed

reference/algorithm/fill_n.md

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,61 @@
55

66
```cpp
77
namespace std {
8-
template <class OutputIterator, class Size, class T>
8+
template <class OutputIterator,
9+
class Size,
10+
class T>
911
void
1012
fill_n(OutputIterator first,
1113
Size n,
1214
const T& value); // (1) C++03
13-
14-
template <class OutputIterator, class Size, class T>
15+
template <class OutputIterator,
16+
class Size,
17+
class T>
1518
OutputIterator
1619
fill_n(OutputIterator first,
1720
Size n,
1821
const T& value); // (1) C++11
19-
20-
template <class OutputIterator, class Size, class T>
22+
template <class OutputIterator,
23+
class Size,
24+
class T>
2125
constexpr OutputIterator
2226
fill_n(OutputIterator first,
2327
Size n,
2428
const T& value); // (1) C++20
29+
template <class OutputIterator,
30+
class Size,
31+
class T = typename iterator_traits<OutputIterator>::value_type>
32+
constexpr OutputIterator
33+
fill_n(OutputIterator first,
34+
Size n,
35+
const T& value); // (1) C++26
2536

26-
template <class ExecutionPolicy, class ForwardIterator,
27-
class Size, class T>
37+
template <class ExecutionPolicy,
38+
class ForwardIterator,
39+
class Size,
40+
class T>
2841
ForwardIterator
2942
fill_n(ExecutionPolicy&& exec,
3043
ForwardIterator first,
3144
Size n,
3245
const T& value); // (2) C++17
46+
template <class ExecutionPolicy,
47+
class ForwardIterator,
48+
class Size,
49+
class T = typename iterator_traits<ForwardIterator>::value_type>
50+
ForwardIterator
51+
fill_n(ExecutionPolicy&& exec,
52+
ForwardIterator first,
53+
Size n,
54+
const T& value); // (2) C++26
3355
}
3456
```
3557
3658
## 概要
3759
イテレータ範囲`[first, first + n)`のすべての要素に指定された値を書き込む。
3860
3961
40-
## 要件
62+
## 適格要件
4163
- `value` は `output iterator` へ書き込み可能でなければならない。
4264
- `Size` は `integral type` に変換可能でなければならない。
4365
@@ -57,24 +79,69 @@ namespace std {
5779
`n` が 1 以上の場合は `n` 回、そうでない場合は 0 回の代入を行う。
5880
5981
82+
## 備考
83+
- (1), (2) :
84+
- C++26 : `value`パラメータとして波カッコ初期化`{}`を受け付ける
85+
```cpp
86+
std::vector<T> v;
87+
std::fill_n(v.begin(), n, {a, b});
88+
```
89+
90+
6091
## 例
92+
### 基本的な使い方
6193
```cpp example
6294
#include <algorithm>
6395
#include <iostream>
6496
#include <iterator>
6597
6698
int main() {
67-
// 3 を出力しまくる
99+
// 値3を10個出力する
68100
std::fill_n(std::ostream_iterator<int>(std::cout, ","), 10, 3);
69101
}
70102
```
71103
* std::fill_n[color ff0000]
72104

73-
### 出力
105+
#### 出力
74106
```
75107
3,3,3,3,3,3,3,3,3,3,
76108
```
77109

110+
### 波カッコ初期化を入力として使用する (C++26)
111+
```cpp example
112+
#include <algorithm>
113+
#include <iostream>
114+
#include <vector>
115+
116+
struct Point {
117+
int x;
118+
int y;
119+
120+
bool operator==(const Point& other) const = default;
121+
};
122+
123+
int main() {
124+
std::vector<Point> v(5);
125+
126+
// 先頭3個の要素を値{1, 2}で埋める
127+
std::fill_n(v.begin(), 3, {1, 2});
128+
129+
for (const Point& p : v) {
130+
std::cout << p.x << "," << p.y << std::endl;
131+
}
132+
}
133+
```
134+
* std::fill_n[color ff0000]
135+
136+
#### 出力
137+
```
138+
1,2
139+
1,2
140+
1,2
141+
0,0
142+
0,0
143+
```
144+
78145
79146
## 実装例
80147
```cpp
@@ -107,3 +174,5 @@ fill_n(OutputIterator first, Size n, const T& value) {
107174
戻り値が追加されるきっかけとなったレポート
108175
- [P0202R3 Add Constexpr Modifiers to Functions in `<algorithm>` and `<utility>` Headers](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html)
109176
- [P0467R2 Iterator Concerns for Parallel Algorithms](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0467r2.html)
177+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
178+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/ranges_fill_n.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@
66

77
```cpp
88
namespace std::ranges {
9-
template<class T, output_iterator<const T&> O>
9+
template <class T,
10+
output_iterator<const T&> O>
1011
constexpr O
11-
fill_n(O first, iter_difference_t<O> n, const T& value); // (1) C++20
12+
fill_n(O first,
13+
iter_difference_t<O> n,
14+
const T& value); // (1) C++20
15+
template <class O,
16+
class T = iter_value_t<O>>
17+
requires output_iterator<O, const T&>
18+
constexpr O
19+
fill_n(O first,
20+
iter_difference_t<O> n,
21+
const T& value); // (1) C++26
1222
}
1323
```
1424
* output_iterator[link /reference/iterator/output_iterator.md]
@@ -30,24 +40,69 @@ namespace std::ranges {
3040
`n` が 1 以上の場合は `n` 回、そうでない場合は 0 回の代入を行う。
3141
3242
43+
## 備考
44+
- (1) :
45+
- C++26 : `value`パラメータとして波カッコ初期化`{}`を受け付ける
46+
```cpp
47+
std::vector<T> v;
48+
std::ranges::fill_n(v.begin(), n, {a, b});
49+
```
50+
51+
3352
## 例
53+
### 基本的な使い方
3454
```cpp example
3555
#include <algorithm>
3656
#include <iostream>
3757
#include <iterator>
3858
3959
int main() {
40-
// 3 を10回出力する
60+
// 値3を10個出力する
4161
std::ranges::fill_n(std::ostream_iterator<int>(std::cout, ","), 10, 3);
4262
}
4363
```
4464
* std::ranges::fill_n[color ff0000]
4565

46-
### 出力
66+
#### 出力
4767
```
4868
3,3,3,3,3,3,3,3,3,3,
4969
```
5070

71+
### 波カッコ初期化を入力として使用する (C++26)
72+
```cpp example
73+
#include <algorithm>
74+
#include <iostream>
75+
#include <vector>
76+
77+
struct Point {
78+
int x;
79+
int y;
80+
81+
bool operator==(const Point& other) const = default;
82+
};
83+
84+
int main() {
85+
std::vector<Point> v(5);
86+
87+
// 先頭3個の要素を値{1, 2}で埋める
88+
std::ranges::fill_n(v.begin(), 3, {1, 2});
89+
90+
for (const Point& p : v) {
91+
std::cout << p.x << "," << p.y << std::endl;
92+
}
93+
}
94+
```
95+
* std::ranges::fill_n[color ff0000]
96+
97+
#### 出力
98+
```
99+
1,2
100+
1,2
101+
1,2
102+
0,0
103+
0,0
104+
```
105+
51106
52107
## バージョン
53108
### 言語
@@ -61,3 +116,5 @@ int main() {
61116
62117
## 参照
63118
- [N4861 25 Algorithms library](https://timsong-cpp.github.io/cppwp/n4861/algorithms)
119+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
120+
- C++26で波カッコ初期化 (リスト初期化) に対応した

0 commit comments

Comments
 (0)