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.
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.
This is the most terrifying and ridiculous thing I've seen in my entire life.
If only the people who want to take on challenges like this put their skills to something actually useful....... There has GOT to be a better use of your knowledge and skills.
Say what?
About semi-complex programs ending up re-implementing aspects of lisp or smalltalk-- just less elegantly.
I think you've done it!
This is proof as to exactly why C++ is such an abomination. I prefer languages that encourage elegant designs.
Nonfunctional programming is not really a different paradigm.
He presumably means "just like" in the same sense as in the phrase, "Eating a high end steak dinner is just like having someone saw through your kneecaps with a circular saw".
Once you get over the syntax, C++ metaprogramming is just like functional programming.
No, the syntax is the entire thing wrong with C++ metaprogramming. It clearly wasn't designed for things like this. To clear up any doubt about the syntax, check out this file. In addition the inscrutable error messages (although those have improved over the past decade). If anyone likes template metaprogramming, I suggest they just use LISP.
That said, this project is really cool. Nice hack mattbierner.
"First they came for the slanderers and i said nothing."
metaprogramming is like functional programming in these senses:
1) Uses lots of recusion
2) Has no variables, only constants. Nothing is mutable.
3) Has functions
Because of 1 and 2, it helps to think in terms of functional programming if you want to do anything substantial.
"First they came for the slanderers and i said nothing."
I thought Tetris issued cease and desist letters for any implementation of its game, even if only for educational purposes.
No, I don't wish compiling were more "fun". Like taking out the trash, or pruning my rose bushes, I want compiling to be over and done with as soon as possible so that I can get on with solving problems.
I especially don't want to have to BE a compiler writer, or try to divine the twisted mind of a compiler writer to solve problems. But that is exactly what you have to be, and how you have to think to use C++.
Which is why I won't touch it with a 10 foot pole.
Terrible language, desperately grasping for relevance in a world that passed it by long ago.
If you want functional programming, USE A FUNCTIONAL PROGRAMMING LANGUAGE.
When I had a $100 steak in Las Vegas, it melted like butter in my mouth. I don't think taking a circular saw to your kneecaps has the same pleasant experience.
Oh goody. So it's no different than any other monadic polymorphized differential functor system utilizing monoidal categories and parameterized applicative type expressions.
Whew. Lucky me. I was worried about finding something to do over the holiday weekend.
Maw! Fire up the karma burner!
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.
Reference: The Tetris Company's victory in court three years ago
How complicated can you make a simple state machine? The implementation is completely misguided.
an ill wind that blows no good
That just just it. I remember Alexander Alexandrescu talking about such stupidity on Usenet in the mid90's. The code without a doubt is complex. slow and resource hogging since you use the compiler as an interpreter. Just a way of wasting watts ( the power it takes the computer to run ). No serious professional programmer would even try to attempt such a thing and there is really no reason to. It also flies in the face of things that Stroustrup and Sutter have been saying at conferences like CppCon2014 to keep things simple. I also remember Scott meyer making a joke about someone noticing the template system being turing complete.
C++ TMP is turing complete and has no state. So yes it is functional. The point is not to write games though, but to write better tuned library code. I can only guess at the illicit substances the person doing this was ingesting.
This confused me so much, I concurrently shot in my shorts and threw up in my mouth.
Has no variables, only constants. Nothing is mutable.
And... this is the definition of functional programming (referential transparency). So yes - it actually really *just like* functional programming, because it *is* functional programming.
I've done that sort of metaprogramming. Years ago I wrote a compile-time Lisp interpreter. It's a HORRIBLE language.
Look, say you have some advanced feature. You could write a library in scheme - it will take you 1 day to a week.
You could write it in C++ templates. It will have a worse feature set than the scheme version, it will be much less readable (not that scheme is readable) and much harder to use. It will have unusable error messages. And a mockup version will take you months to write.
Getting your mockup embedded in Boost and working well with it will take the help of a bunch of experts and a two or three times as much work.
If you want it included in the Boost libraries, you'll need a couple years of work integrating it and getting it approved.
Horrible.
someone was asking for the luajit implementer, Mike Pall, to add copying data structures as something optimized by the compiler and he was told that not sharing structures was a "code smell"
Oh God. The implementer is so out of touch and focused on low level efficiency that he doesn't recognize the basics of reliable programming.
Some time ago Elon Musk went on a rant about AI's destroying us.
AI's I do not fear, but C++ templates... there he might have something.
"There is more worth loving than we have strength to love." - Brian Jay Stanley
What the fuck is "metaprograming"?
Sometimes you have to ask yourself, just because I CAN do this ... SHOULD I do this?
No.
In this case, the answer is clearly no.
Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
The title of this article is a little misleading, as this program works fine with the latest release of gcc (5.1) as well...
[No changes, either to the program or the command-line are required, just use "g++" instead of "clang++".]
Presumably it will also work with any compiler that supports a recent-enough version of the C++ standard and its proposed updates (with the command-line options updated accordingly).
We live, as we dream -- alone....
Template metaprogramming is exactly functional programming.
I got enlightened when I realized that C++ template metaprogramming is actually writing Prolog with a funny syntax.
This is like "functional" programming about as much as playing hopscotch all the way to the next town is like taking the train.
... there really shouldn't be any.
I think the whole template 'metaprogramming' mess can be implemented
very well and readably by normal functions. After all, the only real difference
is that stuff is (partially) evaluated at compile time.
'template double power(double x)' would transform into:
double power(double x, constexpr int N);
'template void swap(T& x, T& y)' would become: /* any type */&y) { // ...
void swap(/* any type */ &x,
assert_sametype(x, y);
assert_moveable(x);
}
etc.
Expanding the idea, things that should logically be possible but are illegal with
current template syntax become trivial, e.g. templatizing on non-integral constants.
Even user-defined types become first-class citizens:
template void f() becomes: void f(constexpr double x);
struct Rectangle { int w; int h; };
template void clip() -> void clip(constexpr Rectangle R);
BTW, variables are over-rated, everything should be const unless specified
otherwise (use mutable, for instance).
I hope I can prove my point sometimes by designing that language, :)
but I'd need time and money...
I don't see why you can't have multistage compiling so that you can use the full power of (whatever language) at "compile" time.
You should be able to have mutable data structures at compile time.
You should be able to have tables and arrays.
And custom file and network io.
Security hole? Like running a bash script isn't a security hole.
void foo(bool on) {
if(on){
#define FOO
} else {
#undef FOO
}
}