C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç¨åºæ¯æå·¥å ·
- std::initializer_list
- 彿°å¯¹è±¡
- std::less
- std::mem_fn
- std::bind
- std::function
- std::not_fn
- std::bind_front
- std::function çæ¨å¯¼æå¼
- std::swap(std::function)
- std::uses_allocator<std::function>
- std::function<R(Args...)>::assign
- std::function<R(Args...)>::operator bool
- std::function<R(Args...)>::operator()
- std::function<R(Args...)>::target_type
- std::function<R(Args...)>::target
- operator==,!=(std::function)
- std::function<R(Args...)>::operator=
- std::function<R(Args...)>::swap
- std::function<R(Args...)>::function
- std::function<R(Args...)>::~function
- std::bad_function_call
- std::is_bind_expression
- std::is_placeholder
- std::placeholders::_1, std::placeholders::_2, ..., std::placeholders::_N
- std::invoke
- std::boyer_moore_searcher
- std::default_searcher
- std::identity
- std::reference_wrapper
- std::ref, std::cref
- std::unwrap_reference, std::unwrap_ref_decay
- std::plus
- std::minus
- std::negate
- std::multiplies
- std::divides
- std::modulus
- std::logical_and
- std::logical_or
- std::logical_not
- std::equal_to
- std::not_equal_to
- std::greater
- std::greater_equal
- std::less_equal
- std::bit_and
- std::bit_or
- std::bit_xor
- std::bit_not
- std::boyer_moore_horspool_searcher
- std::unary_function
- std::binary_function
- std::ptr_fun
- std::pointer_to_unary_function
- std::pointer_to_binary_function
- std::mem_fun_t, std::mem_fun1_t, std::const_mem_fun_t, std::const_mem_fun1_t
- std::not1
- std::not2
- std::bind1st, std::bind2nd
- std::mem_fun_ref_t, std::mem_fun1_ref_t, std::const_mem_fun_ref_t, std::const_mem_fun1_ref_t
- std::unary_negate
- std::binary_negate
- std::less<void>
- std::apply
- åºç¹æ§æµè¯å® (C++20)
- std::hash
- std::pair
- std::tuple
- std::optional
- std::any
- std::variant
- æ ¼å¼ååº (C++20)
- std::integer_sequence
- std::exchange
- std::make_from_tuple
- std::launder
- std::to_chars
- std::from_chars
- std::as_const
- std::source_location
- å忰彿°
- std::bitset
- std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
- std::in_range
- std::declval
- std::forward
- std::move
- std::move_if_noexcept
- std::chars_format
- std::piecewise_construct_t
- std::piecewise_construct
- std::in_place, std::in_place_type, std::in_place_index, std::in_place_t, std::in_place_type_t, std::in_place_index_t
- 注é
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >å·¥å ·åº >彿°å¯¹è±¡ >std::function > std::function<R(Args...)>::function
std::function<R(Args...)>::function
| function() noexcept; |
(1) | (C++11 èµ·) |
| function( std::nullptr_t ) noexcept; |
(2) | (C++11 èµ·) |
| function( const function& other ); |
(3) | (C++11 èµ·) |
| (4) | ||
| function( function&& other ); |
(C++11 èµ·) (C++20 å) |
|
| function( function&& other ) noexcept; |
(C++20 èµ·) | |
| template< class F > function( F f ); |
(5) | (C++11 èµ·) |
| template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc ) noexcept; |
(6) | (C++11 èµ·) (C++17 ä¸ç§»é¤) |
| template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc, |
(7) | (C++11 èµ·) (C++17 ä¸ç§»é¤) |
| template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc, |
(8) | (C++11 èµ·) (C++17 ä¸ç§»é¤) |
| template< class Alloc > function( std::allocator_arg_t, const Alloc& alloc, |
(9) | (C++11 èµ·) (C++17 ä¸ç§»é¤) |
| template< class F, class Alloc > function( std::allocator_arg_t, const Alloc& alloc, F f ); |
(10) | (C++11 èµ·) (C++17 ä¸ç§»é¤) |
ä»åç§èµæºæé std::function ã
function ãother çç®æ å° *this çç®æ ãè¥ other 为空ï¼åè°ç¨å *this å°äº¦ä¸ºç©ºãå¯¹äº (4) ï¼ other å¨è°ç¨åå¤äºåæ³ä½æªæå®çç¶æãf æ¯ç©ºæéææåæåç空æéï¼å *this 卿¤è°ç¨åå°ä¸ºç©ºãæ¤æé 彿°ä¸åä¸éè½½å³è®®ï¼é¤é f 对äºåæ°ç±»å Args... åè¿åç±»å R å¯è°ç¨ (Callable) ã (C++14 èµ·)alloc ç¨äºåé
ä»»ä½ function ä¼ç¨å°çå
鍿°æ®ç»æçå
åãç®æ æ¯å½æ°æéæ std::reference_wrapper æ¶ï¼ä¿è¯ä½¿ç¨å°å¯¹è±¡ä¼åï¼å³å§ç»ç´æ¥åå¨è¿äºç®æ äº std::function 对象ä¸ï¼ä¸åç卿å ååé ãå¯ä»¥æé å ¶ä»å¤§å¯¹è±¡äºå¨æåé çåå¨ä¸ï¼å¹¶ç± std::function 对象éè¿æé访é®ã
åæ°
| other | - | ç¨äºåå§å *this ç彿°å¯¹è±¡ |
| f | - | ç¨äºåå§å *this çå¯è°ç¨å¯¹è±¡ |
| alloc | - | ç¨äºå é¨å ååé çåé å¨ (Allocator) |
| ç±»åè¦æ± | ||
-F å¿
须满足å¯è°ç¨ (Callable) å å¯å¤å¶æé (CopyConstructible) çè¦æ±ã
| ||
-Alloc å¿
须满足åé
å¨ (Allocator) çè¦æ±ã
| ||
å¼å¸¸
other çç®æ æ¯å½æ°æéæ std::reference_wrapper å䏿åºå¼å¸¸ï¼å¦åå¯è½æåº std::bad_alloc æç¨äºå¤å¶æç§»å¨åå¨çå¯è°ç¨å¯¹è±¡çæé 彿°ææçå¼å¸¸ã
|
4) è¥
other çç®æ æ¯å½æ°æéæ std::reference_wrapper å䏿åºå¼å¸¸ï¼å¦åå¯è½æåº std::bad_alloc æç¨äºå¤å¶æç§»å¨åå¨çå¯è°ç¨å¯¹è±¡çæé 彿°ææçå¼å¸¸ã |
(C++20 å) |
f æ¯å½æ°æéæ std::reference_wrapper å䏿å¼å¸¸ï¼å¦åå¯è½æåº std::bad_alloc æåå¨çå¯è°ç¨å¯¹è±¡çå¤å¶æé 彿°ææçå¼å¸¸ã注æ
std::function çåé
卿¯æè¯´æè´«ä¹ï¼ä¸å®ç°ä¸ä¸è´ãä¸äºå®ç°å®å
¨ä¸æä¾éè½½ (6-10) ï¼ä¸äºæä¾éè½½ä½å¿½ç¥æä¾çåé
å¨åæ°ï¼èä¸äºå®ç°æä¾éè½½å¹¶å°æä¾çåé
å¨ç¨äºæé ï¼ä½ä¸å¨éèµå¼ std::function æ¶ä½¿ç¨ãç»æï¼ C++17 ä¸ç§»é¤äºåé
卿¯æã
示ä¾
| æ¬èæªå®æ åå ï¼ææ ç¤ºä¾ |