

Stupid C++ Template Tricks: Snake - adamnemecek
http://blog.mattbierner.com/stupid-template-tricks-snake/

======
amelius
Why not write a compiler that performs the metaprogramming?

The input for this compiler could be in some clean, well thought-out language,
and the output could be dirty C++ template metaprogramming code.

~~~
pbsd
I have seen more than one such compiler, usually from Haskell-like dialects to
C++ metafunctions (e.g.,
[http://gergo.erdi.hu/projects/metafun/](http://gergo.erdi.hu/projects/metafun/)).

In this case the metaprogramming seems unnecessary---even assuming that it
makes any sense to run a snake game at compile time---since C++14's relaxed
`constexpr` functions are a much more readable way to achieve the same thing.
Metaprogramming is only required now when _types_ need to be computed; most of
everything else is more cleanly done with `constexpr`.

~~~
sukilot
Constexpr is C++11, I think?

~~~
pbsd
It is. The original article is already using several other C++11 features,
such as variadic templates and alias templates.

------
striking
This is what happens when you embed a Turing-complete language inside your
compiler. People write programs against the compiler instead of the language.

My question is, is there a real use-case for Turing-completeness inside a C++
compiler?

~~~
jkrems
> My question is, is there a real use-case for Turing-completeness inside a
> C++ compiler?

That's a weird question because it seems to imply that "turing completeness"
was a design goal of C++ templates. Afaik that's not the case, it's just an
emergent feature of a powerful meta programming system. Also, turing-
completeness doesn't mean much. It just means that there is _some_
transformation that allows to general purpose programs to be written. It does
not mean however, that it's practical, performant, or advisable. Things that
are turing-complete include:

* Apache mod_rewrite * The Scala type system * Sendmail configuration * Magic the Gathering

So, being turing-complete really is more of an interesting factoid, not
actually a major achievement with any consequences for "real life".

See:
[http://beza1e1.tuxen.de/articles/accidentally_turing_complet...](http://beza1e1.tuxen.de/articles/accidentally_turing_complete.html)

~~~
lern_too_spel
C++ compile times were bad enough before people realized that it's impossible
to compute in some cases whether their compilation will ever complete.

~~~
jkrems
Except real, contemporary C++ compilers do not implement templates as turing
complete:

    
    
       error: template instantiation depth exceeds maximum of 500 (use -ftemplate-depth-NN to increase the maximum) instantiating ‘struct a<500>’

