Mission Impossible / How to create datatypes which cannot contain invalid state

adrianimboden
4,723 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

For details, see: https://en.cppreference.com/w/cpp/utility/optional

std::optional
#include <iostream>
#include <optional>
void print_value(std::optional<int> optional_value) {
if (optional_value) {
std::cout << optional_value.value() << "\n";
} else {
std::cout << "n/a\n";
}
}
std::optional<int> read_number() {
return 5;
}
int main() {
auto input_number = std::optional<int>{};
print_value(input_number);
input_number = 5;
print_value(input_number);
if (auto opt_num = read_number()) {
std::cout << opt_num.value() << "\n";
}
std::cout << read_number().value_or(7) << "\n";
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content