std::copyable_function::copyable_function
来自 zh.cppreference.net
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>,
std::initializer_list<U> il, CArgs&&... args );
|
(7) | (C++26 起) |
构造新的 std::copyable_function。
1,2) 默认构造函数和接受
nullptr 的构造函数构造空的 std::copyable_function。3) 复制构造函数构造一个
std::copyable_function,其目标为 other 的目标的副本。否则,如果 std::copyable_function 为空,则构造空的 other。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> 依赖于 cv、ref 和 noex,如下表 std::copyable_function 模板参数所示:
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 | - | 要包装的函数或 可调用对象 |
| args | - | 用于构造目标对象的参数 |
| il | - | 用于构造目标对象的 std::initializer_list |
异常
3)
分配失败时可能抛出
std::bad_alloc
,或传播目标对象初始化时抛出的异常。
示例
|
本节内容不完整
原因:缺少示例 |
参见
构造新的
std::function
实例
(
std::function<R(Args...)>
的公开成员函数)
|
|
构造新的
std::move_only_function
对象
(
std::move_only_function
的公开成员函数)
|