Namespaces
Variants

std::function_ref:: function_ref

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
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 为空指针则行为未定义。
  • 仅当 std:: is_function_v < F > /*is-invocable-using*/ < F > 均为 true 时,此重载参与重载决议。
2) 使用 std:: addressof ( f ) 初始化 bound-entity ,并使用函数 thunk 的地址初始化 thunk-ptr
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
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

参数

other - 待复制的另一个 function_ref
f - 要包装的函数或 Callable 对象
obj - 要绑定的对象或指针

示例

另请参阅

构造新的 std::move_only_function 对象
( std::move_only_function 的公开成员函数)