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::bad_function_call
- std::is_bind_expression
- std::is_placeholder
- std::placeholders::_1, std::placeholders::_2, ..., std::placeholders::_N
- std::invoke
- std::not_fn
- std::bind_front
- 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::reference_wrapper
std::reference_wrapper
| å®ä¹äºå¤´æä»¶ <functional>
|
||
| template< class T > class reference_wrapper; |
(C++11 èµ·) | |
std::reference_wrapper æ¯å
è£
å¼ç¨äºå¯å¤å¶ãå¯èµå¼å¯¹è±¡ç类模æ¿ãå®å¸¸ç¨ä½å°å®¹å¨åå¨å
¥æ æ³æ£å¸¸ä¿æå¼ç¨çæ å容å¨ï¼ç±»ä¼¼ std::vector ï¼çæºå¶ã
ç¹å«æ¯ï¼ std::reference_wrapper æ¯å´ç»å°ç±»å T ç对象å¼ç¨æå½æ°å¼ç¨çå¯å¤å¶æé (CopyConstructible) ä¸å¯å¤å¶èµå¼ (CopyAssignable) çå
è£
å¨ã std::reference_wrapper çå®ä¾æ¯å¯¹è±¡ï¼å®ä»¬å¯è¢«å¤å¶æåå¨äºå®¹å¨ï¼ï¼ä½å®ä»¬è½éå¼è½¬æ¢æ T& ï¼æ
è½ä»¥ä¹ä¸ºä»¥å¼ç¨æ¥æ¶åºå±ç±»åç彿°çåæ°ã
è¥åå¨çå¼ç¨å¯è°ç¨ (Callable) ï¼åå¯ä»¥ç¸ååæ°è°ç¨ std::reference_wrapper ã
è¾
å©å½æ° std::ref ä¸ std::cref 常ç¨äºçæ std::reference_wrapper 对象ã
std::reference_wrapper 亦ç¨äºæå¼ç¨ä¼ éå¯¹è±¡ç» std::bind æ std::thread çæé 彿°ã
|
ä¿è¯ |
(C++17 èµ·) |
|
|
(C++20 èµ·) |
æåç±»å
| Â | |
| ç±»å | å®ä¹ |
type
|
T
|
result_type(C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
è¥ T ä¸ºå½æ°å为 T çè¿åç±»åãå¦åä¸å®ä¹ã
|
argument_type(C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
1) è¥ T 为æ¥åä¸ä¸ª A1 ç±»ååæ°ç彿°ææå彿°æéï¼å argument_type 为 A1 ã2) è¥ |
first_argument_type(C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
1) è¥ T æ¯æ¥æ¶äºä¸ª A1 ä¸ A2 ç±»ååæ°ç彿°ææå彿°æéï¼å first_argument_type 为 A1 ã2) è¥ |
second_argument_type(C++17 ä¸å¼ç¨)(C++20 ä¸ç§»é¤)
|
1) è¥ T æ¯æ¥æ¶äºä¸ª A1 ä¸ A2 ç±»ååæ°ç彿°ææå彿°æéï¼å second_argument_type 为 A2 ã2) è¥ |
æå彿°
| åå¨å¼ç¨äºæ°ç std::reference_wrapper 对象 (å ¬å¼æå彿°) | |
| éç»å® std::reference_wrapper (å ¬å¼æå彿°) | |
| 访é®åå¨çå¼ç¨ (å ¬å¼æå彿°) | |
| è°ç¨å
¶æåå¨ç彿° (å ¬å¼æå彿°) |
æ¨å¯¼æå¼(C++17 èµ·)
å¯è½çå®ç°
namespace detail { template <class T> T& FUN(T& t) noexcept { return t; } template <class T> void FUN(T&&) = delete; }  template <class T> class reference_wrapper { public: // ç±»å typedef T type;  // æé /å¤å¶/鿝 template <class U, class = decltype( detail::FUN<T>(std::declval<U>()), std::enable_if_t<!std::is_same_v<reference_wrapper, std::remove_cvref_t<U>>>() )> constexpr reference_wrapper(U&& u) noexcept(noexcept(detail::FUN<T>(std::forward<U>(u)))) : _ptr(std::addressof(detail::FUN<T>(std::forward<U>(u)))) {} reference_wrapper(const reference_wrapper&) noexcept = default;  // èµå¼ reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;  // è®¿é® constexpr operator T& () const noexcept { return *_ptr; } constexpr T& get() const noexcept { return *_ptr; }  template< class... ArgTypes > constexpr std::invoke_result_t<T&, ArgTypes...> operator() ( ArgTypes&&... args ) const { return std::invoke(get(), std::forward<ArgTypes>(args)...); }  private: T* _ptr; };  // æ¨å¯¼æå¼ template<class T> reference_wrapper(T&) -> reference_wrapper<T>; |
示ä¾
æ¼ç¤ºå° reference_wrapper ä½ä¸ºå¼ç¨ç容å¨ä½¿ç¨ï¼è¿ä»¤ç¨å¤é䏿 访é®åä¸å®¹å¨å¯è¡
#include <algorithm> #include <list> #include <vector> #include <iostream> #include <numeric> #include <random> #include <functional>  int main() { std::list<int> l(10); std::iota(l.begin(), l.end(), -4);  std::vector<std::reference_wrapper<int>> v(l.begin(), l.end()); // ä¸è½å¨ list ä¸ç¨ shuffle ï¼è¦æ±éæºè®¿é®ï¼ï¼ä½è½å¨ vector ä¸ä½¿ç¨å® std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});  std::cout << "Contents of the list: "; for (int n : l) std::cout << n << ' '; std::cout << '\n';  std::cout << "Contents of the list, as seen through a shuffled vector: "; for (int i : v) std::cout << i << ' '; std::cout << '\n';  std::cout << "Doubling the values in the initial list...\n"; for (int& i : l) { i *= 2; }  std::cout << "Contents of the list, as seen through a shuffled vector: "; for (int i : v) std::cout << i << ' '; std::cout << '\n'; }
å¯è½çè¾åºï¼
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4 Doubling the values in the initial list... Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8
åé
| (C++11)(C++11) |
å建å
ᾊȌ
¶å®åæ¨å¯¼çç±»åç std::reference_wrapper (彿°æ¨¡æ¿) |
| (C++11) |
ç»å®ä¸æå¤ä¸ªå®åå°å½æ°å¯¹è±¡ (彿°æ¨¡æ¿) |