November 17, 2025Nov 17 If you’re a regular reader of Sandor's blog, you know he's been sharing what he learned about new C++ language and library features ever since C++20. You probably also read his CppCon 2025 Trip Report. And this post is where the two come together. C++26: std::optional<T&> by Sandor Dargo From the article: At CppCon I attended a great talk by Steve Downey about std::optional<T&>. Steve is the father of optional references—he co-authored P2988R12 with Peter Sommerlad. Let’s start with a little history. By now — at the end of 2025 — even C++17 feels like history. That’s when std::optional<T> was introduced, giving us a way to represent “maybe a value” with value semantics, instead of relying on pointers. But std::optional in C++17 (and later) couldn’t hold references — unless you wrapped them in std::reference_wrapper. C++26 finally fixes this gap. What is std::optional<T&>? std::optional<T&> has three key characteristics: Unlike std::optional<T>, it is not an owning type. It simply refers to an existing object. It provides both reference and value-like semantics. Internally, it behaves like a pointer to T, which may also be nullptr. This last point is interesting: while optional<T> can be seen as variant<T, monostate>, optional<T&> is closer to variant<T&, nullptr_t>. In practice, it’s a safer alternative to a non-owning raw pointer. View the full article
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.