std::function::function
function(); | (1) | (since C++11) |
---|---|---|
function( std::nullptr_t ); | (2) | (since C++11) |
function( const function& other ); | (3) | (since C++11) |
function( function&& other ); | (4) | (since C++11) |
template< class F > function( F f ); | (5) | (since C++11) |
template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc ); | (6) | (since C++11) (until C++17) |
template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc, std::nullptr_t ); | (7) | (since C++11) (until C++17) |
template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc, const function& other ); | (8) | (since C++11) (until C++17) |
template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc, function&& other ); | (9) | (since C++11) (until C++17) |
template< class F, class Alloc > function( std::allocator_arg_t, const Alloc& alloc, F f ); | (10) | (since C++11) (until C++17) |
构造一个std::function
来自不同的来源。
1-2%29创建一个空空功能。
3-4%29拷贝%283%29或移动%284%29目标成other
到目标成*this
.如果other
是空空,,,*this
将是空空也是在电话之后。%284%29,other
在调用后处于有效但未指定的状态。
5%29初始化目标带着一份f
.如果f
是指向函数的空指针或指向成员的空指针,*this
将是空空打完电话后。此构造函数不参与重载解析,除非f是Callable
用于参数类型Args...
返回类型R
.%28自C++14%29
6-10%29与%281-5%29相同,但alloc
对象的任何内部数据结构分配内存。function
可能会用。
当目标是函数指针或std::reference_wrapper
,就可以保证小对象优化,也就是说,这些目标总是直接存储在std::function
对象时,不进行动态分配。其他大型对象可以在动态分配的存储中构造,并由std::function
通过指针初始化。
参数
other | - | the function object used to initialize *this |
---|---|---|
f | - | a callable used to initialize *this |
alloc | - | an Allocator used for internal memory allocation |
类型要求
-F必须符合可调用和可复制的要求。
-分配器必须符合分配器的要求。
例外
1-2%29
noexcept
规格:
noexcept
3,4,8,9%29不抛other
%27目标是函数指针或std::reference_wrapper
,否则可能会抛出std::bad_alloc
或用于复制或移动存储的可调用对象的构造函数引发的任何异常。
5,10%29不抛f
是函数指针或std::reference_wrapper
,否则可能会抛出std::bad_alloc
或存储的可调用对象的复制构造函数引发的任何异常。
6-7%29
noexcept
规格:
noexcept
例
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com