|
| 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) |
0 commit comments