The Many Shades of reference_wrapper
summary
std::reference_wrapper models a rebindable reference that mimics language reference semantics while being a small pointer-sized type. It binds only to lvalues, accepts const and incomplete types, converts to T& and exposes assign through via get() so code can both rebind and assign through the same handle. It simplifies algorithms like linked-list remove by replacing pointer-to-pointer idioms and acts as a callable with constexpr callable like function pointers and constrained operator(), but it does not erase target types like std::function. Performance is roughly pointer-cost.
AI-written summary · about
chapters (12)
- 0:00Introduction to reference_wrapper
- 1:56Why not use pointers
- 3:42Introducing reference_wrapper
- 8:47Assign-through and rebinding
- 10:13Linked list node removal
- 13:24Simplifying with reference_wrapper
- 17:05Reading file into linked list
- 18:36Function pointers comparison
- 20:46Reference wrapper as function pointer
- 23:55Type erasure limitations
- 25:20Summary and future
- 26:45Q&A session
related talks
01h 27mUniversal/Forwarding References - A Key to More Modern C++
01h 29mHigher-order functions and function_ref
37mC++ Zero Overhead Pass by Value Through Invocable C++ Abstractions
49mBack to Basics: Type Erasure
01h 41mC++ Type Erasure Demystified
32mBeyond the &: A Future for Native Smart Pointers in Rust
01h 16mCallable Objects in Java, C#, Rust, and C++
31mCo- and Contra-: Adding a Little Variance
