#include #include #include struct Node; struct Number { double value; }; struct BinaryOp { char op; std::unique_ptr left; std::unique_ptr right; }; struct Node { std::variant expr; double eval() const { struct Visitor { double operator()(const Number& n) const { return n.value; } double operator()(const BinaryOp& b) const { double l = b.left->eval(); double r = b.right->eval(); return b.op == '+' ? l + r : 0.0; } }; return std::visit(Visitor{}, expr); } }; int main() { // Build (1 + 2) + 3 = 6 auto leaf1 = std::make_unique(Node{Number{1}}); auto leaf2 = std::make_unique(Node{Number{2}}); auto inner = std::make_unique(Node{BinaryOp{'+', std::move(leaf1), std::move(leaf2)}}); auto root = std::make_unique(Node{BinaryOp{'+', std::move(inner), std::make_unique(Node{Number{3}})}}); std::println("result: {}", root->eval()); }