std::function_ref::function_ref
来自cppreference.com
< cpp | utility | functional | function ref
template< class F > function_ref( F* f ) noexcept; |
(1) | (C++26 起) |
template< class F > function_ref( F&& f ) noexcept; |
(2) | (C++26 起) |
| template< auto f > function_ref( std::nontype_t<f> ) noexcept; |
(3) | (C++26 起) |
template< auto f, class U > function_ref( std::nontype_t<f>, U&& obj ) noexcept; |
(4) | (C++26 起) |
template< auto f, class T > function_ref( std::nontype_t<f>, /* cv */ T* obj ) noexcept; |
(5) | (C++26 起) |
| function_ref( const function_ref& other ) = default; |
(6) | (C++26 起) |
创建新的 std::function_ref。
1) 以
f 初始化 bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr。如果 f 是空指针则其行为未定义。
- 此重载只有在both std::is_function_v<F> and /*is-invocable-using*/<F> are true时才会参与重载决议.
2) 以 std::addressof(f) 初始化
bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr。
- 令 T 为 std::remove_reference_t<F>。此重载只有在:
- std::remove_cvref_t<F> 与
function_ref不是相同类型, - std::is_member_pointer_v<T> 为 false,并且
- /*is-invocable-using*/</*cv*/ T&> 为 true
- std::remove_cvref_t<F> 与
3) 以指向某个未指定对象的指针或空指针值初始化
bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr。
- 令 F 为 decltype(f)。此重载只有在 /*is-invocable-using*/<F> 为 true 时才会参与重载决议。
- 如果当 std::is_pointer_v<F> || std::is_member_pointer_v<F> 为 true 时 f != nullptr 为 false,则程序非良构。
4) 以 std::addressof(obj) 初始化
bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr。
- 令 T 为 std::remove_reference_t<U> 并令 F 为 decltype(f)。此重载只有在:
- std::is_rvalue_reference_v<U&&> 为 false,并且
- /*is-invocable-using*/<F, /*cv*/ T&> 为 true
时才会参与重载决议。
- 如果当 std::is_pointer_v<F> || std::is_member_pointer_v<F> 为 true 时 f != nullptr 为 false,则程序非良构。
5) 以 obj 初始化
bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr。如果当 std::is_member_pointer_v<F> 为 true 时 obj 是空指针,则其行为未定义。
- 令 F 为 decltype(f)。此重载只有在/*is-invocable-using*/<F, /*cv*/ T*> 为 true时才会参与重载决议。
- 如果当 std::is_pointer_v<F> || std::is_member_pointer_v<F> 为 true 时 f != nullptr 为 false,则程序非良构。
6) 预置的复制构造函数复制 other 的
bound-entity 和 thunk-ptr。由某个函数 thunk 的地址来初始化 thunk-ptr,使得对 thunk(bound-entity, call-args...) 的调用表达式等价于:
| 重载 | 表达式等价 |
|---|---|
| (1,3) | std::invoke_r<R>(f, call-args...)
|
| (2) | std::invoke_r<R>(static_cast<cv T&>(f), call-args...)
|
| (4) | std::invoke_r<R>(f, static_cast<cv T&>(obj), call-args...)
|
| (5) | std::invoke_r<R>(f, obj, call-args...)
|
当且仅当以下情况下 /*is-invocable-using*/<T...> 为 true:
- 若 noex 为 true 则 std::is_nothrow_invocable_r_v<R, T..., Args...> 为 true,或者
- std::is_invocable_r_v<R, T..., Args...> 为 true
参数
| other | - | 要从之复制的另一 function_ref
|
| f | - | 要包装的函数或可调用 (Callable) 对象 |
| obj | - | 要绑定的对象或指针 |
示例
| 本节未完成 原因:暂无示例 |
参阅
构造新的 std::move_only_function 对象 ( std::move_only_function 的公开成员函数) |