|
|
|
|
|
by andrewg
2179 days ago
|
|
C++17 does - it's called std::variant. std::variant<int, bool, double> options;
options = true;
bool value = std::get<bool>(options);
bool has_bool = std::holds_alternative<bool>(options);
// or test which alternative is held
if (auto i = std::get_if<int>(&options)) {
// do something with int
} else if (auto b = std::get_if<bool>(&options)) {
// do something with bool
} else {
// do something with double
}
|
|
- No pattern matching means your stuck with the awkward if-elseif-else
- It doesn't check that you've accounted for every possible variant.
- You can only hold one variant of each type: you can't have two variants that both contain a string.
- The specific instance isn't it's own type, so you can't implement methods on it.