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.
#include <optional> std::optional<int> parse(const std::string& text);
Try it
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.
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
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)orif (auto v = f()). Usevalue_orwhen 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
Tvalue, and withstd::nullopt. optional<bool>is confusing:if (opt)tests presence, not the boolean. Writeopt.has_value()and*optexplicitly.- No references:
std::optional<T&>is not allowed up to C++23 (C++26 adds it); useT*orstd::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. emplacedestroys any current value and constructs a new one in place, so it works for types that cannot be assigned.- Not an error type:
optionalsays "nothing", not why. Usestd::expectedwhen callers need a reason. - Moved-from optionals still hold a (moved-from) value; call
reset()if you want them empty.