Pattern matching in C++

Viewed 2253

I'm writing a compiler in C++ and as any compiler, it requires an extreme amount of pattern matching and dynamic casts. In languages like Rust, Haskell, and OCaml I can destruct a type easily, like:

match node {
    Binary{ left, right, operator } => { .. }
    _ => { .. }
}

In C++ the best I can do is:

if (auto bin = dynamic_cast<Binary*>(node)) { ... }
else if (...) { ... }

Which is really limited and ugly if you introduce smart pointers into the scene. For example if I need to match 2 things for something:

bool matched = false;
if (auto m1 = dynamic_cast<Foo*>(a)) {
    if (auto m2 = dynamic_cast<Bar*>(b)) {
        matched = true;
    }
}
if (!matched) {
    // This is because C++ does not allow you to declare two variables inside the condition...
}

I know about the Mach7 library but to be honest it seems awful as you need to write metadata for your structures (also as I've noticed it has quite a lot of bugs and limitations).

Is there a way to make these kind of matches more readable?

1 Answers
Related