Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reference/cstdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
| 名前 | 説明 | 対応バージョン |
|------|------|----------------|
| [`bsearch`](cstdlib/bsearch.md) | 二分探索を行う (function) | C++26で`const`版オーバーロードを追加 |
| `qsort` | 範囲の並べ替えを行う (function) | |
| [`qsort`](cstdlib/qsort.md) | 範囲の並べ替えを行う (function) | |


## 整数に対する算術関数
Expand Down
13 changes: 10 additions & 3 deletions reference/cstdlib/bsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ namespace std {
説明用の型`c-compare-pred`・`compare-pred`は、それぞれ`extern "C"`・`extern "C++"`の言語リンケージを持つ比較関数`int(const void*, const void*)`へのポインタ型である。これにより、いずれの言語リンケージの比較関数も渡せる。


## 事前条件
- 配列は、比較関数`compar`が定める順序に従って昇順にソートされていること。
- 配列の要素型について:
- C++20まで : トリビアルな型であること
- C++23 : 要素型に対する要件は削除された(この要件は[`qsort`](qsort.md)にのみ残り、トリビアルにコピー可能な型に緩和された)


## 戻り値
一致する要素が見つかった場合、その要素へのポインタを返す。一致する要素が複数ある場合、いずれが返されるかは未規定である。

Expand All @@ -60,8 +67,6 @@ namespace std {
- `key`が要素と等しい場合 : `0`
- `key`が要素より大きい場合 : 正の値

配列は、この比較関数の順序に従って昇順にソートされていなければならない。


## 備考
この関数は、フリースタンディング処理系でも使用できる。
Expand Down Expand Up @@ -113,10 +118,12 @@ found: 5


## 関連項目
- `qsort`: 範囲の並べ替えを行う
- [`qsort`](qsort.md): 範囲の並べ替えを行う
- [`std::lower_bound`](/reference/algorithm/lower_bound.md): ソート済み範囲から二分探索を行う


## 参照
- [LWG Issue 3521. Overly strict requirements on `qsort` and `bsearch`](https://cplusplus.github.io/LWG/issue3521)
- C++23で、配列の要素型に対する「トリビアルな型であること」という事前条件が`bsearch`から削除された(この要件は`qsort`にのみ残った)
- [P3348R4 C++26 should refer to C23 not C17](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3348r4.pdf)
- C++26で`const`を保持するオーバーロードが追加された
111 changes: 111 additions & 0 deletions reference/cstdlib/qsort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# qsort
* cstdlib[meta header]
* std[meta namespace]
* function[meta id-type]

```cpp
namespace std {
void qsort(void* base,
size_t nmemb,
size_t size,
c-compare-pred* compar); // (1)
void qsort(void* base,
size_t nmemb,
size_t size,
compare-pred* compar); // (2)
}
```
* size_t[link /reference/cstddef/size_t.md]
* c-compare-pred[italic]
* compare-pred[italic]

## 概要
配列を、比較関数`compar`が定める順序に従って昇順にソートする。

`base`が指す`nmemb`個の要素 (各要素のサイズは`size`バイト) からなる配列を、その場でソートする。

説明用の型`c-compare-pred`・`compare-pred`は、それぞれ`extern "C"`・`extern "C++"`の言語リンケージを持つ比較関数`int(const void*, const void*)`へのポインタ型である。これにより、いずれの言語リンケージの比較関数も渡せる。


## 事前条件
`base`が指す配列の要素は、トリビアルにコピー可能 ([`is_trivially_copyable`](/reference/type_traits/is_trivially_copyable.md)) な型であること。

- C++20まで : トリビアルな型であること
- C++23 : トリビアルにコピー可能な型に緩和された


## 効果
`base`が指す配列の`nmemb`個の要素を、比較関数`compar`が定める順序に従って昇順にソートする。ソートは配列をその場で書き換えることによって行われる。

`compar`は任意の2要素`x`、`y`について、`x`が`y`より前に位置すべき場合に負の値を返すように、全順序と整合する必要がある。同じ配列に対する`compar`の呼び出しは、比較される要素の値のみによって一貫した結果を返さなければならない。

比較関数`compar`が同順とした(`0`を返した)要素どうしの、ソート後の相対順序は規定されない(安定ソートではない)。


## 戻り値
なし


## 比較関数
`compar`は、配列の2つの要素へのポインタを引数に取り、以下を返す関数である。

- 第1引数が第2引数より小さい場合 : 負の値
- 第1引数が第2引数と等しい場合 : `0`
- 第1引数が第2引数より大きい場合 : 正の値


## 例外
比較関数`compar`が送出した例外を送出する。


## 備考
この関数は、フリースタンディング処理系でも使用できる。


## 例
```cpp example
#include <cstdlib>
#include <iostream>
#include <iterator>

int compare(const void* a, const void* b)
{
int x = *static_cast<const int*>(a);
int y = *static_cast<const int*>(b);
return x - y;
}

int main()
{
int data[] = {5, 3, 9, 1, 7};

std::qsort(data, std::size(data), sizeof(int), compare);

for (int x : data) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
```
* std::qsort[color ff0000]
* std::size[link /reference/iterator/size.md]

### 出力
```
1 3 5 7 9
```


## バージョン
### 言語
- C++98


## 関連項目
- [`bsearch`](bsearch.md): ソート済み範囲から二分探索を行う
- [`std::sort`](/reference/algorithm/sort.md): 範囲の並べ替えを行う


## 参照
- [LWG Issue 3521. Overly strict requirements on `qsort` and `bsearch`](https://cplusplus.github.io/LWG/issue3521)
- C++23で、配列の要素型の事前条件がトリビアルな型からトリビアルにコピー可能な型へ緩和された
6 changes: 4 additions & 2 deletions reference/format/formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ namespace std {
2. 式 `cf.format(t, fc)` が有効であり、
- 戻り値の型が`FC::iterator`である
- フォーマット結果を`fc.out()`へ出力し、出力後のイテレータを返す
- 出力は`t`、`fc.locale()`、最後に呼び出された`f.parse(pc)`のイテレータ範囲`[pc.begin(), pc.end())`以外に依存しない
- 出力は`t`、`fc.locale()`、`fc.arg(n)`(任意の`n`について)、最後に呼び出された`f.parse(pc)`のイテレータ範囲`[pc.begin(), pc.end())`以外に依存しない
3. 式 `cf.format(u, fc)` が有効であり、
- 戻り値が`FC::iterator`である
- フォーマット結果を`fc.out()`へ出力し、出力後のイテレータを返す
- 出力は`u`、`fc.locale()`、最後に呼び出された`f.parse(pc)`のイテレータ範囲`[pc.begin(), pc.end())`以外に依存しない
- 出力は`u`、`fc.locale()`、`fc.arg(n)`(任意の`n`について)、最後に呼び出された`f.parse(pc)`のイテレータ範囲`[pc.begin(), pc.end())`以外に依存しない
- `u`を変更しない

条件内の各要素を、以下のように定義する。
Expand Down Expand Up @@ -284,6 +284,8 @@ int main()
- [P2286R8 Formatting Ranges](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2286r8.html)
- [P2585R1 Improve default container formatting](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2585r1.html)
- C++23から、Range・コンテナ、`pair`、`tuple`のフォーマット出力、および文字・文字列のデバッグ指定 (`"?"`) が追加された
- [LWG Issue 3462. §[formatter.requirements]: Formatter requirements forbid use of `fc.arg()`](https://cplusplus.github.io/LWG/issue3462)
- C++23で、`format`の出力が依存してよい対象に`fc.arg(n)`が追加され、動的な幅・精度指定などをフォーマッターから実装できるようになった
- [LWG Issue 3701. Make `formatter<remove_cvref_t<const charT[N]>, charT>` requirement explicit](https://cplusplus.github.io/LWG/issue3701)
- C++23で、有効な標準特殊化の一覧に`formatter<charT[N], charT>`が明示的に追加された
- [LWG Issue 3833. Remove specialization `template<size_t N> struct formatter<const charT[N], charT>`](https://cplusplus.github.io/LWG/issue3833)
Expand Down
11 changes: 9 additions & 2 deletions reference/forward_list/forward_list/unique.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ constexpr size_type unique(BinaryPredicate pred); // (2) C++26
コンテナがソート済みであること。ソート済みでない場合、この関数の動作は未規定。


## 事前条件
- (2) :
- C++23 : `pred`は同値関係であること。


## 効果
イテレータ範囲`[first + 1, last)`の全てのイテレータ`i`について、
連続する等価な要素のグループごとに、先頭の要素以外を削除する。すなわち、イテレータ範囲`[first + 1, last)`の全てのイテレータ`i`について、

- (1) : `*i == *(i - 1)`
- (2) : `pred(*i, *(i - 1))`
Expand All @@ -48,7 +53,7 @@ constexpr size_type unique(BinaryPredicate pred); // (2) C++26


## 計算量
ちょうど`(last - first) - 1`回の等値比較、もしくは述語の適用を行う。
コンテナが空でない場合、ちょうど`(last - first) - 1`回の等値比較、もしくは述語の適用を行う。空の場合、等値比較・述語の適用は行わない


## 例
Expand Down Expand Up @@ -91,4 +96,6 @@ int main()

## 参照
- [P0646R1 Improving the Return Value of Erase-Like Algorithms I: list/forward list](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0646r1.pdf)
- [LWG Issue 2997. LWG 491 and the specification of `{forward_,}list::unique`](https://cplusplus.github.io/LWG/issue2997)
- C++23で、`pred`が同値関係であるという事前条件が追加され、効果・計算量の記述が整理された
- [P3372R3 constexpr containers and adaptors](https://open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3372r3.html)
9 changes: 8 additions & 1 deletion reference/fstream/basic_filebuf/open.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ basic_filebuf* open(const filesystem::path::value_type* s,
ios_base::openmode mode); // (2) C++17
basic_filebuf* open(const string& s, ios_base::openmode mode); // (3)
basic_filebuf* open(const filesystem::path& s, ios_base::openmode mode); // (4) C++17
template <class T>
basic_filebuf* open(const T& s, ios_base::openmode mode); // (4) C++23
```

## 概要

- (1): `s`で指定されたファイルを開く。`s`はヌル終端文字列。
- (2): [`std::filesystem::path::value_type`](/reference/filesystem/path.md)の型が`char`ではないときのみ定義される。効果は(1)と同じ。
- (3): ファイルを指定する引数の型が`std::string`である点を除き、(1)と同じ。
- (4): ファイルを指定する引数の型が[`std::filesystem::path`](/reference/filesystem/path.md)である点を除き、(1)と同じ。
- (4): ファイルを指定する引数の型が[`std::filesystem::path`](/reference/filesystem/path.md)である点を除き、(1)と同じ。C++23では、`path`へ暗黙変換可能な型([`std::string_view`](/reference/string_view/basic_string_view.md)など)が渡されたときの高コストな暗黙変換を防ぐため、`filesystem::path`とまったく同じ型のみを受け取る制約付きテンプレートとして定義される。

## テンプレートパラメータ制約
- (4) C++23 : [`is_same_v`](/reference/type_traits/is_same.md)`<T, filesystem::path>`が`true`であること

## 効果

Expand Down Expand Up @@ -85,4 +90,6 @@ int main()
## 参照

- [LGW issue 2676. Provide filesystem::path overloads for File-based streams](https://wg21.cmeerw.net/lwg/issue2676)
- [LWG Issue 3430. `std::fstream` & co. should be constructible from `string_view`](https://cplusplus.github.io/LWG/issue3430)
- C++23で、`filesystem::path`を受け取る`open`(4)が、`path`へ暗黙変換可能な型による高コストな変換を防ぐため、`is_same_v<T, filesystem::path>`を制約とする制約付きテンプレートに変更された
- [LGW issue 2943. Problematic specification of the wide version of basic_filebuf::open](https://wg21.cmeerw.net/lwg/issue2943)
10 changes: 9 additions & 1 deletion reference/fstream/basic_fstream/op_constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ explicit basic_fstream(const filesystem::path::value_type* s,
ios_base::openmode mode = ios_base::in|ios_base::out); // (4) C++17
explicit basic_fstream(const filesystem::path& s,
ios_base::openmode mode = ios_base::in | ios_base::out); // (5) C++17
template <class T>
explicit basic_fstream(const T& s,
ios_base::openmode mode = ios_base::in | ios_base::out); // (5) C++23
basic_fstream(const basic_fstream& rhs) = delete; // (6) C++11
basic_fstream(basic_fstream&& rhs); // (7) C++11
```

## 概要
オブジェクトを構築する。一部のオーバーロードでは、ファイルを開く機能を持っている。

## テンプレートパラメータ制約
- (5) C++23 : [`is_same_v`](/reference/type_traits/is_same.md)`<T, filesystem::path>`が`true`であること

## 効果

- (1) : デフォルトコンストラクタ。空の状態にする。
- (2) : 仮引数`s`で指定したファイルを開く。
- [`rdbuf()->open(s, mode)`](/reference/fstream/basic_filebuf/open.md)を呼び出す。その結果が失敗だった(戻り値がヌルポインタだった)場合、[`setstate(failbit)`](/reference/ios/basic_ios/setstate.md)を呼び出す。
- (3) : ファイルを指定する引数の型が`std::string`である点を除き、(2)と同じ。
- (4) : [`std::filesystem::path::value_type`](/reference/filesystem/path.md)の型が`char`ではないときのみ定義される。効果は(2)と同じ。
- (5) : ファイルを指定する引数の型が[`std::filesystem::path`](/reference/filesystem/path.md)である点を除き、(2)と同じ。
- (5) : ファイルを指定する引数の型が[`std::filesystem::path`](/reference/filesystem/path.md)である点を除き、(2)と同じ。C++23では、`path`へ暗黙変換可能な型([`std::string_view`](/reference/string_view/basic_string_view.md)など)が渡されたときの高コストな暗黙変換を防ぐため、`filesystem::path`とまったく同じ型のみを受け取る制約付きテンプレートとして定義される。
- (6) : コピーコンストラクタ。コピー不可。
- (7) : ムーブコンストラクタ。ファイルストリームの所有権を移動する。

Expand Down Expand Up @@ -109,3 +115,5 @@ basic_fstream<CharT, Traits>::basic_fstream(basic_fstream&& rhs)

- [LGW issue 2676. Provide filesystem::path overloads for File-based streams](https://wg21.cmeerw.net/lwg/issue2676)
- [LWG Issue 3130. §[input.output] needs many `addressof`](https://wg21.cmeerw.net/lwg/issue3130)
- [LWG Issue 3430. `std::fstream` & co. should be constructible from `string_view`](https://cplusplus.github.io/LWG/issue3430)
- C++23で、`filesystem::path`を受け取るコンストラクタ(5)が、`path`へ暗黙変換可能な型による高コストな変換を防ぐため、`is_same_v<T, filesystem::path>`を制約とする制約付きテンプレートに変更された
11 changes: 10 additions & 1 deletion reference/fstream/basic_fstream/open.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@ void open(
void open(
const filesystem::path& s,
ios_base::openmode mode = ios_base::in | ios_base::out); // (4) C++17
template <class T>
void open(
const T& s,
ios_base::openmode mode = ios_base::in | ios_base::out); // (4) C++23
```

## 概要

ファイルを開く

## テンプレートパラメータ制約
- (4) C++23 : [`is_same_v`](/reference/type_traits/is_same.md)`<T, filesystem::path>`が`true`であること

## 効果

- (1) : 仮引数`s`で指定したファイルを開く。
- [`rdbuf()->open(s, mode)`](/reference/fstream/basic_filebuf/open.md)を呼び出す。その結果が成功だった(戻り値がヌルポインタではなかった)場合、[`clear()`](/reference/ios/basic_ios/clear.md)を呼び出す。その結果が失敗だった(戻り値がヌルポインタだった)場合、[`setstate(failbit)`](/reference/ios/basic_ios/setstate.md)を呼び出す。
- (2) : [`std::filesystem::path::value_type`](/reference/filesystem/path.md)の型が`char`ではないときのみ定義される。効果は(1)と同じ。
- (3) : ファイルを指定する引数の型が`std::string`である点を除き、(1)と同じ。
- (4) : ファイルを指定する引数の型が[`std::filesystem::path`](/reference/filesystem/path.md)である点を除き、(1)と同じ。
- (4) : ファイルを指定する引数の型が[`std::filesystem::path`](/reference/filesystem/path.md)である点を除き、(1)と同じ。C++23では、`path`へ暗黙変換可能な型([`std::string_view`](/reference/string_view/basic_string_view.md)など)が渡されたときの高コストな暗黙変換を防ぐため、`filesystem::path`とまったく同じ型のみを受け取る制約付きテンプレートとして定義される。

## 例

Expand Down Expand Up @@ -73,3 +80,5 @@ int main()
## 参照

- [LGW issue 2676. Provide filesystem::path overloads for File-based streams](https://wg21.cmeerw.net/lwg/issue2676)
- [LWG Issue 3430. `std::fstream` & co. should be constructible from `string_view`](https://cplusplus.github.io/LWG/issue3430)
- C++23で、`filesystem::path`を受け取る`open`(4)が、`path`へ暗黙変換可能な型による高コストな変換を防ぐため、`is_same_v<T, filesystem::path>`を制約とする制約付きテンプレートに変更された
Loading
Loading