Using optionals to represent the absence of a value
suggest changeBefore C++17, having pointers with a value of nullptr
commonly represented the absence of a value. This is a good solution for large objects that have been dynamically allocated and are already managed by pointers. However, this solution does not work well for small or primitive types such as int
, which are rarely ever dynamically allocated or managed by pointers. std::optional
provides a viable solution to this common problem.
In this example, struct Person
is defined. It is possible for a person to have a pet, but not necessary. Therefore, the pet
member of Person
is declared with an std::optional
wrapper.
#include <iostream>
#include <optional>
#include <string>
struct Animal {
std::string name;
};
struct Person {
std::string name;
std::optional<Animal> pet;
};
int main() {
Person person;
person.name = "John";
if (person.pet) {
std::cout << person.name << "'s pet's name is " <<
person.pet->name << std::endl;
}
else {
std::cout << person.name << " is alone." << std::endl;
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents