“Do not use owning raw pointers, use a smart pointer instead.” — And yet it is common to use them when writing a linked list, for example.
“Use a reference when a pointer is non-null.” — But the standard library interfaces themselves don’t follow this advice all the time.
We’re at a point where a simple question “I need to point to some other object” requires a complicated lecture about raw vs. smart pointers, references and the danger of putting references inside angle brackets. Clearly we can do better.
Let’s take a step back, and look at the bigger picture: What are the common pointer-like types? Which properties do they have in common, which are unique? What are the possible situations that require pointer-like types? Which properties do those situations require?
This talk will answer those questions and provide the definitive guide to choosing the correct pointer types.
We’ll be discussing whether or not optional<T&>
or std::observer_ptr
are necessary,
talk about the difference in applications for T&
, gsl::non_null<T>
and std::reference_wrapper<T>
,
and look how the type system can help us catch lifetime issues.
Important: Read this blog post for a revised terminology — A (Better) Taxonomy of Pointers.