Tech News
← Back to articles

Lisp-style C++ template meta programming

read original related products more articles

Lispy Meta Programming

C++ template meta programming in a Lisp style.

C++17 is needed.

This is currently a demo just for proof of concept.

See test.cc for examples.

Example: Sieve of Eratosthenes

# include < lmp.h > using namespace lmp ; meta_fn (infinite_integers, int n) { // `let_lazy(name, expr)` is similar to `(define name (delay expr))` in scheme let_lazy (next, infinite_integers); meta_return (cons, next>); }; meta_fn (filter_mod, class lst , int n) { let_lazy (tail, filter_mod, n>); meta_return ( cond, Int>, Int< 0 >>, tail, cons, tail>>); }; meta_fn (prime_sieve, class lst ) { static constexpr int n = car::value; let_lazy (tail, prime_sieve, n>>); meta_return (cons, tail>); }; using primes = prime_sieve>; static_assert (nth::type::value == 2 ); static_assert (nth::type::value == 3 ); static_assert (nth::type::value == 5 ); static_assert (nth::type::value == 7 ); static_assert (nth::type::value == 11 ); static_assert (nth::type::value == 13 );

Similar form in Scheme: