Slashdot Mirror


Clang Plays Tetris -- Tetris As a C++ Template Metaprogram

New submitter mjvzb writes: Ever wish compiling was more fun? Well, I recently implemented Tetris as a C++ template metaprogram (code at Github). The game is played by recompiling its source, taking player input by compiler flag. The runtime program is only needed to print the game screen to the console and save the game state across compiler runs.

Implementing Tetris in templates is not as horrific as you may imagine, and I've put together a post covering the details. Once you get over the syntax, C++ metaprogramming is just like functional programming.

1 of 68 comments (clear)

  1. Re:C++ metaprogramming is just like functional pro by serviscope_minor · · Score: 3, Informative

    Say what?

    Template metaprogramming is exactly functional programming.

    Not only that, C++98 style was completely 100% pure with no mutabe state. You got recuursion and pattern matching (called [partial] specialisation). A class is essentially a function returning a type (well, multiple types) as internal typedefs. And as such functions are very much first class objects.

    C++11 added some local mutable state. constexpr functions can now be evaluated at compile time. Those yielding an int can be used as template arguments.

    --
    SJW n. One who posts facts.