# Using C++17 std::optional

DevFeed: [Using C++17 std::optional](<https://devfeed.tech/articles/using-c-17-std-optional-61110.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2018/05/using-optional/>)

Published: 2026-09-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [Programming](<https://devfeed.tech/topics/programming.md>), [Memory Safety](<https://devfeed.tech/topics/memory-safety.md>), [union types](<https://devfeed.tech/topics/union-types.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-20](<https://devfeed.tech/tags/c-plus-plus-20.md>), [c-plus-plus-23](<https://devfeed.tech/tags/c-plus-plus-23.md>), [c-plus-plus-26](<https://devfeed.tech/tags/c-plus-plus-26.md>), [haskell](<https://devfeed.tech/tags/haskell.md>), [null](<https://devfeed.tech/tags/null.md>), [programming](<https://devfeed.tech/tags/programming.md>), [rust](<https://devfeed.tech/tags/rust.md>), [type-safety](<https://devfeed.tech/tags/type-safety.md>)

## AI overview

This tutorial explains std::optional, a C++17 value type that represents a value that may be absent. It discusses its type safety, memory behavior, and uses such as nullable values, computation results, lazy resource loading, and optional function parameters.

## Source excerpt

Let's take a pair of two types <YourType, bool> - what can you do with such composition? In this article, I'll describe std::optional - a helper "vocabulary" type added in C++17. It's a wrapper that either contains a value of your type or is empty. Let's see where it can be useful and how you can use it. Updated in September 2026 with C++20, C++23, and C++26 changes. Intro By adding the boolean flag to other types, you can achieve a thing called "nullable types". As mentioned, the flag is used to indicate whether the value is available or not. Such wrapper represents an object that might be empty in an expressive way (so not via comments :)) While you can achieve "null-ability" by using unique values (-1, infinity, nullptr), it's not as clear as the separate wrapper type. Alternatively, you could even use std::unique_ptr<Type> and treat the empty pointer as not initialized - this works, but comes with the cost of allocating memory for the object. Optional types - that come from functional programming world - bring type safety and expressiveness. Most of other languages have something similar: for example std::option in Rust, Optional<T> in Java, Data.Maybe in Haskell. std::optional was added in C++17 and brings a lot of experience from boost::optional that was available for many years. Since C++17 you can just #include <optional> and use the type. Such wrapper is still a value type (so you can copy it, via deep copy). What's more, std::optional doesn't need to allocate any memory on the free store. std::optional is a part of C++ vocabulary types along with std::any, std::variant and std::string_view. When to use Usually, you can use an optional wrapper in the following scenarios: If you want to represent a nullable type nicely. Rather than using unique values (like -1, nullptr, NO_VALUE or something) For example, user's middle name is optional. You could assume that an empty string would work here, but knowing if a user entered something or not might be important. W