std::function_ref::function_ref

来自cppreference.com
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
通透运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧式绑定器与适配器
(C++17 前*)
(C++17 前*)
(C++17 前*)
(C++17 前*)
(C++17 前*)(C++17 前*)(C++17 前*)(C++17 前*)
(C++20 前*)
(C++20 前*)
(C++17 前*)(C++17 前*)
(C++17 前*)(C++17 前*)

(C++17 前*)
(C++17 前*)(C++17 前*)(C++17 前*)(C++17 前*)
(C++20 前*)
(C++20 前*)
 
 
<tbody> </tbody>
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
  • Tstd::remove_reference_t<F>。此重载只有在:
    • std::remove_cvref_t<F>function_ref 不是相同类型,
    • std::is_member_pointer_v<T>false,并且
    • /*is-invocable-using*/</*cv*/ T&>true
时才会参与重载决议。
3) 以指向某个未指定对象的指针或空指针值初始化 bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr
  • Fdecltype(f)。此重载只有在 /*is-invocable-using*/<F>true 时才会参与重载决议。
  • 如果当 std::is_pointer_v<F> || std::is_member_pointer_v<F>truef != nullptrfalse,则程序非良构。
4)std::addressof(obj) 初始化 bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr
  • Tstd::remove_reference_t<U> 并令 Fdecltype(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>truef != nullptrfalse,则程序非良构。
5)obj 初始化 bound-entity,并以某个函数 thunk 的地址初始化 thunk-ptr。如果当 std::is_member_pointer_v<F>trueobj 是空指针,则其行为未定义。
  • Fdecltype(f)。此重载只有在/*is-invocable-using*/<F, /*cv*/ T*>true时才会参与重载决议。
  • 如果当 std::is_pointer_v<F> || std::is_member_pointer_v<F>truef != nullptrfalse,则程序非良构。
6) 预置的复制构造函数复制 otherbound-entitythunk-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<cvT&>(f), call-args...)
(4) std::invoke_r<R>(f, static_cast<cvT&>(obj), call-args...)
(5) std::invoke_r<R>(f, obj, call-args...)

当且仅当以下情况下 /*is-invocable-using*/<T...>true

  • noextruestd::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 的公开成员函数) [编辑]