Skip to content

Commit 4cbbdae

Browse files
committed
functional/function_ref: 概要のみ(#1192)
1 parent a9bd9cf commit 4cbbdae

File tree

8 files changed

+210
-8
lines changed

8 files changed

+210
-8
lines changed

lang/cpp26.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
8686

8787

8888
### 関数オブジェクト
89-
- 所有権を保持しない[`std::function`](/reference/functional/function.md)として、[`<functional>`](/reference/functional.md)[`std::function_ref`](/reference/functional/function_ref.md.nolink)クラスを追加
89+
- 所有権を保持しない[`std::function`](/reference/functional/function.md)として、[`<functional>`](/reference/functional.md)[`std::function_ref`](/reference/functional/function_ref.md)クラスを追加
9090
- [`std::move_only_function`](/reference/functional/move_only_function.md)のコピー可能版として、[`<functional>`](/reference/functional.md)[`std::copyable_function`](/reference/functional/copyable_function.md)クラスを追加
9191
- [`std::bind_front()`](/reference/functional/bind_front.md)[`std::bind_back()`](/reference/functional/bind_back.md.nolink)に、非型テンプレート引数として関数を指定するオーバーロードを追加
9292
- 関連して、非型テンプレート引数の関数オブジェクトを反転させられるよう、[`not_fn()`](/reference/functional/not_fn.md)に非型テンプレート引数版のオーバーロードを追加

reference/functional.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct doubler {
2727
| [`bad_function_call`](functional/bad_function_call.md) | 不正な関数呼び出しに関する例外(class) | C++11 |
2828
| [`move_only_function`](functional/move_only_function.md) | ムーブのみ可能な関数・関数オブジェクトの多相軽量ラッパー(class template) | C++23 |
2929
| [`copyable_function`](functional/copyable_function.md) | コピー可能な関数・関数オブジェクトの多相軽量ラッパー(class template) | C++26 |
30-
| [`function_ref`](functional/function_ref.md.nolink) | 関数・関数オブジェクトの多相参照ラッパー(class template) | C++26 |
30+
| [`function_ref`](functional/function_ref.md) | 関数・関数オブジェクトの多相参照ラッパー(class template) | C++26 |
3131
3232
3333
## 統一的な関数呼び出し

reference/functional/copyable_function.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ namespace std {
3636
3737
3838
## メンバ関数
39-
### 構築・破棄
4039
4140
| 名前 | 説明 | 対応バージョン |
4241
|-----------------|----------------|----------------|
@@ -235,8 +234,9 @@ int main()
235234

236235

237236
## 関連項目
238-
- [`function`](function.md)
239-
- [`move_only_function`](move_only_function.md)
237+
- C++11 [`function`](function.md)
238+
- C++23 [`move_only_function`](move_only_function.md)
239+
- C++26 [`function_ref`](function_ref.md)
240240

241241

242242
## 参照

reference/functional/function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int main()
117117
## 関連項目
118118
- C++23 [`move_only_function`](move_only_function.md)
119119
- C++26 [`copyable_function`](copyable_function.md)
120-
- C++26 [`function_ref`](function_ref.md.nolink)
120+
- C++26 [`function_ref`](function_ref.md)
121121
122122
123123
## 参照
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# function_ref
2+
* functional[meta header]
3+
* class template[meta id-type]
4+
* std[meta namespace]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template<class... S>
10+
class function_ref; // 宣言のみ
11+
12+
template<class R, class... ArgTypes>
13+
class function_ref<R(ArgTypes...) /*cv*/ noexcept(/*noex*/)>;
14+
}
15+
```
16+
17+
## 概要
18+
`function_ref`クラステンプレートは、パラメータの型リスト`ArgTypes...`、戻り値の型`R`に合致する、あらゆる関数ポインタ、関数オブジェクト、メンバ関数ポインタ、メンバ変数ポインタを参照できるクラスである。
19+
20+
下記全ての組み合わせ(4種類)に対して、クラステンプレートの部分特殊化が提供される。
21+
22+
- CV修飾子 *cv* : `const`, CV修飾無し
23+
- noexcept例外指定 *noex* : `true`, `false`
24+
25+
`function_ref`クラステンプレートのあらゆる特殊化は[トリビアルコピー可能](/reference/type_traits/is_trivially_copyable.md)である。
26+
27+
28+
### `function`ファミリとの比較
29+
類似機能を提供する[`function`](function.md), [`move_only_function`](move_only_function.md), [`copyable_function`](copyable_function.md)とは異なり、`function_ref`は下記の特徴をもつ。
30+
31+
- 対象の所有権を管理しない軽量ラッパーとして実装される。
32+
- ポインタ2個のオブジェクトサイズで実装可能。
33+
- 構築時に必ず呼び出し対象を指定する必要がある。デフォルト構築不可。
34+
- `operator bool`を提供しない。
35+
- メンバ関数・メンバ変数を参照する場合は、[`std::nontype`](/reference/utility/nontype_t.md)タグを利用する。
36+
- 対象オブジェクトの束縛タイミングは、構築時または呼び出し時のいずれもサポートする。
37+
- ダングリング(dangling)参照を避けるため、左辺値(lvalue)のみを取り扱う。
38+
39+
40+
## メンバ関数
41+
42+
| 名前 | 説明 | 対応バージョン |
43+
|-----------------|----------------|----------------|
44+
| [`(constructor)`](function_ref/op_constructor.md.nolink) | コンストラクタ | C++26 |
45+
| (destructor) | デストラクタ | C++26 |
46+
| [`operator=`](function_ref/op_assign.md.nolink) | 代入演算子 | C++26 |
47+
| [`operator()`](function_ref/op_call.md.nolink) | 関数呼び出し | C++26 |
48+
49+
50+
## 推論補助
51+
52+
| 名前 | 説明 | 対応バージョン |
53+
|-----------------|----------------|----------------|
54+
| [`(deduction_guide)`](function_ref/op_deduction_guide.md.nolink) | クラステンプレートの推論補助 | C++26 |
55+
56+
57+
## 例
58+
### 例1: 基本の使い方
59+
```cpp example
60+
#include <functional>
61+
#include <iostream>
62+
#include <utility> // nontype
63+
64+
// 呼び出し可能な何かを受け取る高階関数
65+
int hof(std::function_ref<int(int)> fn)
66+
{
67+
return fn(2);
68+
}
69+
70+
int add(int x) { return x + 1; }
71+
72+
struct Calc {
73+
int x_;
74+
int eval(int y) {
75+
return x_ * y;
76+
}
77+
};
78+
79+
int main()
80+
{
81+
// 通常関数を指定
82+
std::cout << hof(add) << std::endl;
83+
// ラムダ式を指定
84+
std::cout << hof([](int x) { return x * 2; }) << std::endl;
85+
86+
// オブジェクト束縛済みメンバ関数を指定
87+
Calc obj{ 3 };
88+
std::cout << hof({std::nontype<&Calc::eval>, obj}) << std::endl;
89+
}
90+
```
91+
* std::function_ref[color ff0000]
92+
* std::nontype[link /reference/utility/nontype_t.md]
93+
94+
#### 出力
95+
```
96+
3
97+
4
98+
6
99+
```
100+
101+
## バージョン
102+
### 言語
103+
- C++26
104+
105+
### 処理系
106+
- [Clang](/implementation.md#clang): ??
107+
- [GCC](/implementation.md#gcc): ??
108+
- [ICC](/implementation.md#icc): ??
109+
- [Visual C++](/implementation.md#visual_cpp): ??
110+
111+
112+
## 関連項目
113+
- C++11 [`function`](function.md)
114+
- C++23 [`move_only_function`](move_only_function.md)
115+
- C++26 [`copyable_function`](copyable_function.md)
116+
117+
118+
## 参照
119+
- [P0792R14 `function_ref`: a type-erased callable reference](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0792r14.html)

reference/functional/move_only_function.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace std {
3737
3838
3939
## メンバ関数
40-
### 構築・破棄
4140
4241
| 名前 | 説明 | 対応バージョン |
4342
|-----------------|----------------|----------------|
@@ -236,7 +235,9 @@ int main()
236235

237236

238237
## 関連項目
239-
- [`function`](function.md)
238+
- C++11 [`function`](function.md)
239+
- C++26 [`copyable_function`](copyable_function.md)
240+
- C++26 [`copyable_function`](copyable_function.md)
240241

241242

242243
## 参照

reference/utility.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
| [`in_place_index`](utility/in_place_index_t.md) | 指定位置にある要素型のコンストラクタ引数を直接受け取って構築するためのタグ値 (constant variable) | C++17 |
9999

100100

101+
## 非型引数
102+
103+
| 名前 | 説明 | 対応バージョン |
104+
|------|------|----------------|
105+
| [`nontype_t`](utility/nontype_t.md) | 非型引数を表現するためのタグ型 (class template) | C++26 |
106+
| [`nontype`](utility/nontype_t.md) | 非型引数を表現するためのタグ値 (variable template) | C++26 |
107+
108+
101109
## コンパイル時の整数シーケンス
102110

103111
| 名前 | 説明 | 対応バージョン |

reference/utility/nontype_t.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# nontype_t
2+
* utility[meta header]
3+
* std[meta namespace]
4+
* class template[meta id-type]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template <auto V>
10+
struct nontype_t {
11+
explicit nontype_t() = default;
12+
};
13+
14+
template <auto V> constexpr nontype_t<V> nontype{};
15+
}
16+
```
17+
18+
## 概要
19+
`nontype_t`クラスは、オーバーロードのための空クラスである。
20+
21+
標準ライブラリの特定機能において、非型引数をテンプレートパラメータとして受け取って構築するための関数オーバーロードを定義するためにある。
22+
23+
24+
## 備考
25+
デフォルトコンストラクタに`explicit`が付いているのは、`nontype_t<F> x = {};`のように`=`付きの波カッコ初期化を禁止するためである。ユーザーは通常、`nontype_t`型の変数テンプレートとして事前定義されている`nontype`を使用すればよいので、問題にはならない。
26+
27+
28+
## 例
29+
```cpp example
30+
#include <functional>
31+
#include <print>
32+
#include <utility>
33+
34+
void call(std::function_ref<void()> fn)
35+
{
36+
fn();
37+
}
38+
39+
struct Dog {
40+
void cry() { std::println("Bow-wow"); }
41+
};
42+
43+
int main()
44+
{
45+
Dog dog;
46+
call({std::nontype<&Dog::cry>, dog});
47+
}
48+
```
49+
* std::nontype[color ff0000]
50+
* std::function_ref[link /reference/functional/function_ref.md]
51+
52+
### 出力
53+
```
54+
Bow-wow
55+
```
56+
57+
58+
## バージョン
59+
### 言語
60+
- C++26
61+
62+
### 処理系
63+
- [Clang](/implementation.md#clang): ??
64+
- [GCC](/implementation.md#gcc): ??
65+
- [ICC](/implementation.md#icc): ??
66+
- [Visual C++](/implementation.md#visual_cpp): ??
67+
68+
69+
## 関連項目
70+
- [`copyable_function`](/reference/functional/copyable_function.md)
71+
72+
73+
## 参照
74+
- [P0792R14 `function_ref`: a type-erased callable reference](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0792r14.html)

0 commit comments

Comments
 (0)