Exploring the power of std::optional in C++

CMP
3 min readJan 11, 2024

--

As the C++ language has evolved over the years, new features and functionality have been added to make programming more efficient and expressive.

What is std::optional?

In C++17, std::optional and the <optional> header were added, allowing programmers to cleanly handle optional values without needing to use some type of programmer-defined special value.

One clear use case for std::optional is for a return value that may fail. Consider the following example:

#include <iostream>…

--

--

CMP

Software engineer specializing in operating systems, navigating the intracicies of the C++ language and systems programming.