正常
Pointer Semantics and Weak Reference Design
Pointers are everywhere in C++, but not all pointers "own" the objects they point to. A raw pointer T* can be either owning or non-owning, T& expresses a borrow but cannot be null, and std::weak_ptr solves the weak reference problem under shared ownership—but what if you don't use std::shared_ptr to manage objects? How is Chromium's base::WeakPtr designed? And why does std::observer_ptr look like a WeakPtr but actually isn't?
In this topic, we build various non-owning pointer types from scratch. As we implement them, we clarify their semantic boundaries, safety conditions, and engineering trade-offs.
Chapter Contents
Non-owning pointer panorama: from T* to Borrowed to ObserverPtrWeakPtr anti-pattern: the fatal trap of T* + raw Flag*SimpleWeakPtr: safe improvements with T* + shared_ptr<Flag>Chrome-like WeakPtr: reference-counted control blocks and WeakPtrFactorystd::weak_ptr comparison and async callback practiceCross-thread safety, performance trade-offs, and design principles summary