I Just Wanted to Point to Something!

Every non-trivial programming language needs a way to refer to another object that is stored in a different place. In some programming languages this behavior is the default ­— they have reference semantics.

But this is not the case in C++. In C++ you need a special type to refer to another object: a pointer. I mean a reference … or std::reference_wrapper? Or maybe a smart pointer. But probably std::string_view if the object is a string. And sometimes an iterator if you’re pointing to anything in a container. Or maybe something completely different.

Confused? Maybe not. Maybe you are experienced and know what to choose. But did you always choose correctly? This talk will provide comprehensive guidelines.

We’ll answer questions like:

  • When should I use a reference, when a pointer, when something different?

  • If references are non-null pointers, what’s the point of gsl::non_null<T*> to create a non-null pointer?

  • Do I need std::optional<T&>? Or is it just a pointer?

  • What the heck is C++17’s std::byte* and when is it useful?

  • Is there still a need for void*?

In the end you’ll know exactly when to use which pointer-like type and why.

Note: I’m much more happy with the version of the talk I gave at C++Now 2018, consider checking it out instead: jonathanmueller.dev/talk/cppnow2018.

See Also