std::copyable_function::copyable_function
来自cppreference.com
< cpp | utility | functional | copyable function
| copyable_function() noexcept; |
(1) | (C++26 起) |
| copyable_function( std::nullptr_t ) noexcept; |
(2) | (C++26 起) |
| copyable_function( const copyable_function& other ); |
(3) | (C++26 起) |
| copyable_function( copyable_function&& other ) noexcept; |
(4) | (C++26 起) |
| template< class F > copyable_function( F&& f ); |
(5) | (C++26 起) |
| template< class T, class... CArgs > explicit copyable_function( std::in_place_type_t<T>, CArgs&&... args ); |
(6) | (C++26 起) |
| template< class T, class U, class... CArgs > explicit copyable_function( std::in_place_type_t<T>, |
(7) | (C++26 起) |
创建新的 std::copyable_function。
1,2) 默认构造函数和接受 nullptr 的构造函数构造空的
std::copyable_function。3) 复制构造函数构造
std::copyable_function,其目标为 other 目标的副本。否则,若 other 为空则构造空的 std::copyable_function。4) 移动构造函数构造
std::copyable_function,其目标为 other 的目标。移动构造之后 other 处于有效但未指明的状态。5) 令
VT 为 std::decay_t<F>。如果 f 是空函数指针,空成员指针值,或者空的 std::copyable_function(可为其他任意特化),那么构造空的 std::copyable_function。否则,构造 std::copyable_function,其目标类型为 VT 并以 std::forward<F>(f) 进行直接非列表初始化。
- 此重载只有在
VT既和copyable_function不同又不是 std::in_place_type_t 的特化,且 /*is-callable-from*/<VT>(见下文)为 true时才会参与重载决议。 - 如果 std::is_constructible_v<VT, F> 或 std::is_copy_constructible_v<VT> 不为 true,那么程序非良构。
6) 令
VT 为 std::decay_t<T>。构造 std::copyable_function,其目标类型为 VT 并以 std::forward<CArgs>(args)... 进行直接非列表初始化。
- 此重载只有在 std::is_constructible_v<VT, CArgs...> 和 /*is-callable-from*/<VT>(见下文)均为 true时才会参与重载决议。
- 如果
VT与T不是同一类型或者 std::is_copy_constructible_v<VT> 不为 true,那么程序非良构。
7) 令
VT 为 std::decay_t<T>。构造 std::copyable_function,其目标类型为 VT 并以 il, std::forward<CArgs>(args)... 进行直接非列表初始化。
- 此重载只有在 std::is_constructible_v<VT, std::initializer_list<U>&, CArgs...> 和 /*is-callable-from*/<VT>(见下文)均为 true时才会参与重载决议。
- 如果
VT与T不是同一类型或者 std::is_copy_constructible_v<VT> 不为 true,那么程序非良构。
对于构造函数 (5-7),除非 VT 同时满足可析构 (Destructible) 和可复制构造 (CopyConstructible) ,否则其行为未定义。
常量 /*is-callable-from*/<VT> 依赖于 std::copyable_function 的模板形参中的 cv、ref 和 noex 如下:
| 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::copyable_function
|
| f | - | 要包装的函数或可调用 (Callable) 对象 |
| args | - | 用以构造目标对象的实参 |
| il | - | 用以构造目标对象的 std::initializer_list |
异常
3) 分配失败时可抛出 std::bad_alloc,或传播目标的初始化所抛出的异常。
示例
| 本节未完成 原因:暂无示例 |
参阅
| 构造新的 std::function 实例 ( std::function<R(Args...)> 的公开成员函数) | |
构造新的 std::move_only_function 对象 ( std::move_only_function 的公开成员函数) |