std::move_only_function::move_only_function

来自cppreference.com
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
旧式绑定器与适配器
(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 前*)
 
 
move_only_function() noexcept;
(1)(C++23 起)
move_only_function( std::nullptr_t ) noexcept;
(2)(C++23 起)
move_only_function( move_only_function&& other ) noexcept;
(3)(C++23 起)
move_only_function( const move_only_function& ) = delete;
(4)(C++23 起)
template< class F >
move_only_function( F&& f );
(5)(C++23 起)
template< class T, class... CArgs >
explicit move_only_function( std::in_place_type_t<T>, CArgs&&... args );
(6)(C++23 起)
template< class T, class U, class... CArgs >

explicit move_only_function( std::in_place_type_t<T>,

                             std::initializer_list<U> il, CArgs&&... args );
(7)(C++23 起)

创建新的 std::move_only_function

1,2) 默认构造函数与接收 nullptr 的构造函数构造空的 std::move_only_function
3) 移动构造函数构造目标为 other 的目标的 std::move_only_function。移动构造后 other 处于合法但未指明的状态。
4) 复制构造函数被弃置。std::move_only_function 不满足可复制构造 (CopyConstructible)
5)VTstd::decay_t<F>。若 f 为空函数指针、空成员指针值或空 std::move_only_function(可为其他任何特化),则构造空的 std::move_only_function。否则,构造目标具有 VT 类型并以 std::forward<F>(f) 直接非列表初始化的 std::move_only_function
  • 此重载只有在 VT 既非 move_only_function 亦非 std::in_place_type_t 的特化,且 /*is-callable-from*/<VT>true 时才会参与重载决议。
  • std::is_constructible_v<VT, F>true 则程序非良构。
6)VTstd::decay_t<T>。构造目标具有 VT 类型并以 std::forward<CArgs>(args)... 直接非列表初始化的 std::move_only_function
  • 此重载只有在 std::is_constructible_v<VT, CArgs...>/*is-callable-from*/<VT> (见后述)均为 true 时才会参与重载决议。
  • VTT 不是同一类型则程序非良构。
7)VTstd::decay_t<T>。构造目标具有 VT 类型并以 il, std::forward<CArgs>(args)... 直接非列表初始化的 std::move_only_function
  • 此重载只有在 std::is_constructible_v<VT, std::initializer_list<U>&, CArgs...>/*is-callable-from*/<VT>(见后述)均为 true 时才会参与重载决议。
  • VTT 不是同一类型则程序非良构。

对于构造函数 (5-7),若 VT 不满足可析构 (Destructible) 要求或若 std::is_move_constructible_v<VT>true 但不满足可移动构造 (MoveConstructible) 要求则行为未定义。

常量 /*is-callable-from*/<VT> 依赖于 std::move_only_function 的模板形参中的 cvrefnoex 如下:

cv ref noexcept(noex)/*is-callable-from*/<VT>
noexcept(false)std::is_invocable_r_v<R, VT, Args...> &&

std::is_invocable_r_v<R, VT&, Args...>

noexcept(true)std::is_nothrow_invocable_r_v<R, VT, Args...> &&

std::is_nothrow_invocable_r_v<R, VT&, Args...>

const noexcept(false)std::is_invocable_r_v<R, const VT, Args...> &&

std::is_invocable_r_v<R, const VT&, Args...>

const noexcept(true)std::is_nothrow_invocable_r_v<R, const VT, Args...> &&

std::is_nothrow_invocable_r_v<R, const VT&, Args...>

& noexcept(false)std::is_invocable_r_v<R, VT&, Args...>
& noexcept(true)std::is_nothrow_invocable_r_v<R, VT&, Args...>
const & noexcept(false)std::is_invocable_r_v<R, const VT&, Args...>
const & noexcept(true)std::is_nothrow_invocable_r_v<R, const VT&, Args...>
&& noexcept(false)std::is_invocable_r_v<R, VT, Args...>
&& noexcept(true)std::is_nothrow_invocable_r_v<R, VT, Args...>
const && noexcept(false)std::is_invocable_r_v<R, const VT, Args...>
const && noexcept(true)std::is_nothrow_invocable_r_v<R, const VT, Args...>

参数

other-要移动的另一 std::move_only_function
f-要包装的函数或可调用 (Callable) 对象
args-构造目标对象的实参
il-构造目标对象的 std::initializer_list

异常

5-7) 可能在分配失败时抛出 std::bad_alloc 或传播初始化目标时抛出的异常。若 VT 为函数指针类型或 std::reference_wrapper 的特化则不抛异常。

示例

参阅

构造新的 std::function 实例
(std::function<R(Args...)> 的公开成员函数)
创建新 std::copyable_function 对象
(std::copyable_function 的公开成员函数)