optional and variant
optional holds a value or nothing. variant holds one of several types.
Example
#include <iostream>
#include <optional>
#include <variant>
#include <string>
int main() {
std::optional<int> o;
std::cout << o.has_value() << " " << o.value_or(7) << "\n";
std::variant<int, std::string> v = "hi";
std::cout << std::get<std::string>(v) << " " << v.index() << "\n";
return 0;
}Result
0 7 hi 1
Exercise
Default value.
o.(7)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.