Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ std::optional class

Reference for std::optional: a value that may be absent, with has_value, value, value_or, emplace, reset, and_then, transform and or_else (C++23).

std::optional<T> (C++17, header <optional>) either holds a T or holds nothing. It replaces sentinel values such as -1, null pointers and out-parameters when a function may legitimately have no result. The value lives inside the optional object itself, so there is no heap allocation.

An optional converts to bool (true when it holds a value) and is dereferenced with * and -> like a pointer, but unlike a pointer it owns the value and copies it when copied. The empty state is spelled std::nullopt.

C++
#include <optional>

std::optional<int> parse(const std::string& text);

Try it

C++ Returning an optional

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Chaining with transform and and_then (C++23)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T> class optional;

constexpr optional() noexcept;
constexpr optional(std::nullopt_t) noexcept;
template<class U = T> constexpr optional(U&& value);
Name Description
T The value type. It must be a destructible object type (not a reference, array or nullopt_t).
value Initial value; the optional then holds a T.
std::nullopt Tag that means "no value". Assigning it, or default construction, makes the optional empty.

Members: has_value, operator bool, value, value_or, operator*, operator->, emplace, reset, swap, and the monadic operations and_then, transform and or_else (all C++23). Free helpers: std::make_optional, comparison operators, std::hash.

Returns: value_or(x) returns the stored value or x converted to T (by copy or move). and_then(f) calls f with the value and returns its optional result, or an empty optional. transform(f) wraps the result of f in a new optional. or_else(f) returns *this when it has a value, otherwise f().

Errors / undefined behavior: value() throws std::bad_optional_access if empty. *opt and opt-> on an empty optional are undefined behavior (no check, even in debug builds of most libraries).

Usage notes

  • Check before dereferencing: if (opt) or if (auto v = f()). Use value_or when a default makes sense.
  • Comparison: an empty optional is less than any engaged one; two empties are equal. An optional compares directly with a plain T value, and with std::nullopt.
  • optional<bool> is confusing: if (opt) tests presence, not the boolean. Write opt.has_value() and *opt explicitly.
  • No references: std::optional<T&> is not allowed up to C++23 (C++26 adds it); use T* or std::optional<std::reference_wrapper<T>>.
  • Size: about sizeof(T) plus a flag, rounded up for alignment. For small types a sentinel value is smaller, but the optional is clearer.
  • emplace destroys any current value and constructs a new one in place, so it works for types that cannot be assigned.
  • Not an error type: optional says "nothing", not why. Use std::expected when callers need a reason.
  • Moved-from optionals still hold a (moved-from) value; call reset() if you want them empty.