Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Tetris as a C++ Template Metaprogram (mattbierner.com)
79 points by ScottWRobinson on July 1, 2015 | hide | past | favorite | 23 comments


I am not sure if I should be amazed or scared by this. Guess it won't be long before somebody comes up with the only thing left to do, going full meta and writing a "c++ to c++ templates compiler" (or something along those lines).

Now if only we had a way to use all this goodness to reflect on structs/enumerate members...

On the actual topic: I wonder how much memory it takes to compile a single round of tetris?


This kind of compiler has already be done: http://gergo.erdi.hu/projects/metafun/


Amazing. Maybe we are onto a new kind of Rule 34 here...

"Everything that can be implemented in {c++ templates, javascript} will eventually be implemented in that language"


Atwood's Law: "Any application that can be written in JavaScript, will eventually be written in JavaScript."


That's hilarious -- in fact the full quote is even better. This guy hits it right on the nose:

jeff atwood: "any application that can be written in JavaScript, will eventually be written in JavaScript. Writing Photoshop, Word, or Excel in JavaScript makes zero engineering sense, but it's inevitable. It will happen. In fact, it's already happening"

Good foresight considering he said this 2009.


Of course, it doesn't make "zero" engineering sense in any logical fashion. Only those emotionally distraught by the "imperfection" of JavaScript don't see the utility in applications moving to the web.

Which isn't to say the web is ready. But it will be!


Yeah moving the software in the cloud (the new name for the Web) is the right way to go... well if you want to end up with a terminal in your hand and the whole intelligence upper in the cloud... like an interactive TV from the nineties or a french Minitel from the eighties. :)


Thankfully, "moving to the web" no longer means JavaScript; WebAssembly can support that kind of thing instead, using arbitrary languages.

Also, you say "emotionally distraught" as though there's no technical reason to dislike JavaScript.


There are technical reasons to dislike literally every technology. I say "emotionally distraught" because JavaScript seems to attract detractors who think that strong feelings are a valid substitute for logic.


I was under the impression that asm.js/emscripten are the new hotness these days? Hence not requiring porting everything to JS manually to bring it "to the web" anymore.


Since jwz said "every program attempts to expand until it can read mail", the only thing we still need is a mail reader in C++ templates.


In lisp it's much easier to do, just write normal tetris and call it at compile-time via macro.


Pics or it didn't happen ;) /jk

On a serious note: modern C++ does have const expression functions, too, albeit not as powerful (yet): http://www.cprogramming.com/c++11/c++11-compile-time-process...


No need for pics, it's the thing about Lisps that you can run arbitrary code during compilation. The distinction between compile-time and runtime is pretty blurred, especially that Lisp programs tend to ship with the compiler in the image, which means you can compile new stuff at runtime.


I realize that. I was joking [more like trying to, it seems] about the fact that parent commented on a technically amazing post with "I could have done this too in X" without providing any further insight, let alone a link to the completed project in X.


Fair enough :). Though I interpret that parent comment not as bragging, but as making a joke that in Lisp you write the program as you would normally, and then just make it run in compile-time :).


This is so cool and so totally and entirely useless that it had to be done. Bravo !


C++ template metaprogramming is turing complete. So why not write a simple compiler that targets it. Then you can write any program as a template metaprogram.



I could have sworn I saw a guy do this over at gamedev.net in 2003. I can't, for the life of me, find it.



Yeah, I remember that specific code (I even remember when it was written, it was quite a good time for all the chaps), but I know that wasn't the template metaprogramming version. Perhaps I'm just remembering a later conversation where we discussed the concept that C++ templates were Turing-complete and thus Tetris could be made, then someone jokingly linked to this.


This is evil




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: