4 ms·
Tetris as a C++ Template Metaprogram
- paulasmuth 11y agoI 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?
- emillon 11y agoThis kind of compiler has already be done: http://gergo.erdi.hu/projects/metafun/ http://gergo.erdi.hu/projects/metafun/
- paulasmuth 11y agoAmazing. 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"
- abhorrence 11y agoAtwood's Law: "Any application that can be written in JavaScript, will eventually be written in JavaScript."
- paulasmuth 11y agoThat'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.
- oldmanjay 11y agoOf 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!
- JoshTriplett 11y agoThankfully, "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.
- oldmanjay 11y agoThere 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.
- paulasmuth 11y agoI 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.
- 0x4a42 11y agoYeah 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. :)
- codeshaman 11y agoThis is so cool and so totally and entirely useless that it had to be done. Bravo !
- avodonosov 11y agoIn lisp it's much easier to do, just write normal tetris and call it at compile-time via macro.
- paulasmuth 11y agoPics 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-processing-with-constexpr.html http://www.cprogramming.com/c++11/c++11-compile-time-process...
- TeMPOraL 11y agoNo 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.
- paulasmuth 11y agoI 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.
- TeMPOraL 11y agoFair 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 :).
- amelius 11y agoC++ 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.
- zem 11y agobeen done: http://gergo.erdi.hu/projects/metafun/ http://gergo.erdi.hu/projects/metafun/
- moron4hire 11y agoI 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.
- Aardwolf 11y agoDo you maybe mean this? :D http://web.archive.org/web/20030309013522/http://www.gamedev.net/columns/hardcore/tetris/ http://web.archive.org/web/20030309013522/http://www.gamedev...
- moron4hire 11y agoYeah, 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.
- gpvos 11y agoSince 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.
- whoseking 11y agoThis is evil