3 ms·
Stupid C++ Template Tricks: Snake
- amelius 12y agoWhy 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.
- lmm 12y agoWhat's the use case? In a scenario where you're able to use another language/tool, why would you want a C++ template metaprogramming stage at all? (The only use cases I'm aware of for C++ template metaprogramming are where you have a hard requirement to do something in standard C++, or for the sake of doing it in C++ template metaprogramming)
- pbsd 12y agoI 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`.
- praptak 12y agoIt's what Lisp does, only both the compiler and the target language is Lisp.
- striking 12y agoThis 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?
- AnthonyMouse 12y ago> My question is, is there a real use-case for Turing-completeness inside a C++ compiler? If it isn't Turing-complete then there are some things you can't compute with it. If you want to do one of those things which could be computed at compile time then without it you would have to compute it at runtime. Which means it has to be computed on every execution rather than once by the compiler and if there is some logic error in the code you may not discover the error until that code is actually executed rather than having the compiler find it for you. It also has the potential to save you a lot of duplication. You're basically asking the compiler to run a program to convert your C++ program into arbitrarily many similar-but-not-quite-the-same C++ programs, algorithmically. Because it's Turing-complete you can do any transformation with it. There is no edge case it can't handle which could cause you to need to write and maintain 500+ nearly identical copies of the same code instead, which is what can happen in e.g. Java.
- jkrems 12y ago> 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_complete.html http://beza1e1.tuxen.de/articles/accidentally_turing_complet...
- 12y ago