Skip to content

Instantly share code, notes, and snippets.

@mkitzan
mkitzan / pattern_matching.cpp
Last active October 13, 2022 23:53
Twisting the C++ constexpr interpreter and template system into performing ML-like pattern matching
/*
This code implements ML like pattern matching in C++17 using constexpr and the template system.
Like in ML, a standalone function evaluates the expression. However, with C++ templates and
constexpr, the evaluation function for the expression can be embedded within the type. Where
every node type has a static member function eval that performs its step of the expression.
See the link below for SQL WHERE predicate nodes implementing this pattern:
https://github.com/mkitzan/metaprogramming-optimization/blob/master/include/sql/predicate.hpp
https://godbolt.org/z/PQnBXf
clang -std=c++17 -lstdc++ -o pm pattern_matching.cpp