Daily bit(e) of C++ | Optional values: std::unique_ptr, std::optional
Daily bit(e) of C++ #101, Representing optional values using std::unique_ptr and std::optional
If we want to represent an optional value, we can use std::unique_ptr (C++11) and heap allocation (nullptr denoting absence). However, this incurs a runtime cost and can fail (and throw).
std::optional (C++17) will not allocate dynamic memory and only incurs the cost of storing a boolean.