|
| 1 | +# コンストラクタ |
| 2 | +* functional[meta header] |
| 3 | +* std[meta namespace] |
| 4 | +* function_ref[meta class] |
| 5 | +* function[meta id-type] |
| 6 | +* cpp26[meta cpp] |
| 7 | + |
| 8 | +```cpp |
| 9 | +template<class F> function_ref(F* f) noexcept; // (1) |
| 10 | + |
| 11 | +template<class F> |
| 12 | +constexpr function_ref(F&& f) noexcept; // (2) |
| 13 | + |
| 14 | +template<auto f> |
| 15 | +constexpr function_ref(nontype_t<f>) noexcept; // (3) |
| 16 | +template<auto f, class U> |
| 17 | +constexpr function_ref(nontype_t<f>, U&& obj) noexcept; // (4) |
| 18 | +template<auto f, class T> |
| 19 | +constexpr function_ref(nontype_t<f>, /*cv*/ T* obj) noexcept; // (5) |
| 20 | + |
| 21 | +constexpr function_ref(const function_ref&) noexcept = default; // (6) |
| 22 | +``` |
| 23 | +* nontype_t[link /reference/utility/nontype_t.md] |
| 24 | +
|
| 25 | +## 概要 |
| 26 | +`function_ref`オブジェクトを構築する。 |
| 27 | +
|
| 28 | +`function_ref`クラステンプレートパラメータのnoexcept例外指定 *noex* に応じて、説明用の`bool`型テンプレート定数`is-invocable-using<T...>`を次のように定義する : |
| 29 | +
|
| 30 | +- *noex* が`true`のとき : [`is_nothrow_invocable_r_v`](/reference/type_traits/is_nothrow_invocable_r.md)`<R, T..., ArgTypes...>` |
| 31 | +- *noex* が`false`のとき : [`is_invocable_r_v`](/reference/type_traits/is_invocable_r.md)`<R, T..., ArgTypes...>` |
| 32 | +
|
| 33 | +`function_ref`オブジェクトは、説明専用のメンバ変数`thunk-ptr`と`bound-entity`を保持する。 |
| 34 | +
|
| 35 | +
|
| 36 | +## テンプレートパラメータ制約 |
| 37 | +- (1) : [`is_function`](/reference/type_traits/is_function.md)`<F>`が`true`、かつ`is-invocable-using<F>`が`true`であること |
| 38 | +- (2) : `T`を`remove_refernce_t<F>`としたとき |
| 39 | + - `remove_cvref_t<F>`が`function_ref`と同一型ではなく、かつ |
| 40 | + - [`is_member_pointer_v`](/reference/type_traits/is_member_pointer.md)`T`が`false`であり、かつ |
| 41 | + - `is-invocable-using</*cv*/ T&>`が`true`であること |
| 42 | +- (3) : `F`を`decltype(f)`としたとき |
| 43 | + - `is-invocable-using<F>`が`true`であること |
| 44 | +- (4) : `T`を`remove_refernce_t<F>`、`F`を`F`を`decltype(f)`としたとき |
| 45 | + - [`is_rvalue_reference_v](/reference/type_traits/is_rvalue_reference.md)`<U&&>`が`false`であり、かつ |
| 46 | + - `is-invocable-using<F, /*cv*/ T&>`が`true`であること |
| 47 | +- (5) : `F`を`decltype(f)`としたとき |
| 48 | + - `is-invocable-using<F, /*cv*/ T*>`が`true`であること |
| 49 | +
|
| 50 | +
|
| 51 | +## 適格要件 |
| 52 | +- (3), (4), (5) : `F`を`decltype(f)`としたとき、[`is_pointer`](/reference/type_traits/is_pointer.md)`<F> ||` [`is_member_pointer`](/reference/type_traits/is_member_pointer.md)`<F>`が`true`ならば、`f`がヌルポインタでないこと。 |
| 53 | +
|
| 54 | +
|
| 55 | +## 事前条件 |
| 56 | +- (1) : `f`がヌルポインタでないこと。 |
| 57 | +
|
| 58 | +
|
| 59 | +## 効果 |
| 60 | +- (1) : `bound-entity`を`f`で、`thunk-ptr`を説明専用の関数`thunk`へのアドレスで初期化する。 |
| 61 | + - [関数呼び出し`thunk(bound-entity, call-args...)`](op_call.md)は[`invoke_r`](/reference/functional/invoke_r.md)`<R>(f, call-args...)`と等価。 |
| 62 | +- (2) : `bound-entity`を[`addressof`](/reference/memory/addressof.md)`(f)`で、`thunk-ptr`を説明専用の関数`thunk`へのアドレスで初期化する。 |
| 63 | + - [関数呼び出し`thunk(bound-entity, call-args...)`](op_call.md)は[`invoke_r`](/reference/functional/invoke_r.md)`<R>(static_cast</*cv*/ T&>(f), call-args...)`と等価。 |
| 64 | +- (3) : `bound-entity`を未規定オブジェクトへのポインタまたはヌルポインタで、`thunk-ptr`を説明専用の関数`thunk`へのアドレスで初期化する。 |
| 65 | + - [関数呼び出し`thunk(bound-entity, call-args...)`](op_call.md)は[`invoke_r`](/reference/functional/invoke_r.md)`<R>(f, call-args...)`と等価。 |
| 66 | +- (4) : `bound-entity`を[`addressof`](/reference/memory/addressof.md)`(obj)`で、`thunk-ptr`を説明専用の関数`thunk`へのアドレスで初期化する。 |
| 67 | + - [関数呼び出し`thunk(bound-entity, call-args...)`](op_call.md)は[`invoke_r`](/reference/functional/invoke_r.md)`<R>(f, static_cast</*cv*/ T&>(obj), call-args...)`と等価。 |
| 68 | +- (5) : `bound-entity`を`obj`で、`thunk-ptr`を説明専用の関数`thunk`へのアドレスで初期化する。 |
| 69 | + - [関数呼び出し`thunk(bound-entity, call-args...)`](op_call.md)は[`invoke_r`](/reference/functional/invoke_r.md)`<R>(f, obj, call-args...)`と等価。 |
| 70 | +- (6) : コピーコンストラクタ。 |
| 71 | +
|
| 72 | +
|
| 73 | +## 例外 |
| 74 | +投げない |
| 75 | +
|
| 76 | +
|
| 77 | +## 例 |
| 78 | +```cpp example |
| 79 | +#include <functional> |
| 80 | +#include <iostream> |
| 81 | +#include <utility> |
| 82 | +
|
| 83 | +int ident_func(int x) |
| 84 | +{ return x; } |
| 85 | +
|
| 86 | +struct ident_functor { |
| 87 | + int operator()(int x) const |
| 88 | + { return x; } |
| 89 | +}; |
| 90 | +
|
| 91 | +struct X { |
| 92 | + int ident_func(int x) const |
| 93 | + { return x; } |
| 94 | +}; |
| 95 | +
|
| 96 | +
|
| 97 | +int main() |
| 98 | +{ |
| 99 | + // (1) 関数ポインタ |
| 100 | + { |
| 101 | + std::function_ref<int(int)> f1 = &ident_func; |
| 102 | + std::cout << "(1) : " << f1(1) << std::endl; |
| 103 | + } |
| 104 | + // (2) 関数オブジェクト |
| 105 | + { |
| 106 | + ident_functor functor; |
| 107 | + std::function_ref<int(int)> f2 = functor; |
| 108 | + std::cout << "(2) : " << f2(2) << std::endl; |
| 109 | + } |
| 110 | + // (3) メンバ関数 |
| 111 | + { |
| 112 | + std::function_ref<int(X&, int)> f3 = std::nontype<&X::ident>; |
| 113 | + X obj; |
| 114 | + std::cout << "(3) : " << f3(obj, 3) << std::endl; |
| 115 | + } |
| 116 | + // (4), (5) メンバ関数+オブジェクト束縛 |
| 117 | + { |
| 118 | + X obj; |
| 119 | + std::function_ref<int(int)> f4{std::nontype<&X::ident>, obj}; |
| 120 | + std::cout << "(4) : " << f4(4) << std::endl; |
| 121 | + std::function_ref<int(int)> f5{std::nontype<&X::ident>, &obj}; |
| 122 | + std::cout << "(5) : " << f5(5) << std::endl; |
| 123 | + } |
| 124 | + // (6) コピーコンストラクタ |
| 125 | + { |
| 126 | + std::function_ref<int(int)> f1 = &ident_func; |
| 127 | + std::function_ref<int(int)> f6 = f1; |
| 128 | + std::cout << "(6) : " << f6(6) << std::endl; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | +* std::nontype[link /reference/utility/nontype_t.md] |
| 133 | + |
| 134 | +### 出力 |
| 135 | +``` |
| 136 | +(1) : 1 |
| 137 | +(2) : 2 |
| 138 | +(3) : 3 |
| 139 | +(4) : 4 |
| 140 | +(5) : 5 |
| 141 | +(6) : 6 |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | +## バージョン |
| 146 | +### 言語 |
| 147 | +- C++26 |
| 148 | + |
| 149 | +### 処理系 |
| 150 | +- [Clang](/implementation.md#clang): ?? |
| 151 | +- [GCC](/implementation.md#gcc): ?? |
| 152 | +- [ICC](/implementation.md#icc): ?? |
| 153 | +- [Visual C++](/implementation.md#visual_cpp): ?? |
| 154 | + |
| 155 | + |
| 156 | +## 参照 |
| 157 | +- [P0792R14 `function_ref`: a type-erased callable reference](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0792r14.html) |
0 commit comments