In the C++ Core Guidelines std::optional
is only referred once:
If you need the notion of an optional value, use a pointer, std::optional, or a special value used to denote “no value.”
Other than that, it is not mentioned in the guidelines, so in particular there is no recommendation to use it instead of a pointer, when expressing the intent of an optional value.
Are there any disadvantages of the usage of std::optional
in comparison to a pointer that might be null in a non-polymorphic context?
So std::optional
is only referred as a side note and as the second option to a pointer.
To me it seems like that std::optional
is much more expressive. When using a pointer, you can never be really sure if the option of a nullptr
is intended.