6 ms·
I still don't understand why people bother with template metaprogramming. And I genuinely mean it. Other languages have much simpler and nicer constructs and se
by emsy 8y ago
I still don't understand why people bother with template metaprogramming. And I genuinely mean it. Other languages have much simpler and nicer constructs and seem to work just fine. And even within C++ you can often solve the problem with more verbose, but simpler code. Every time I read an article or watch a talk on it I have the impression that people use it because they can, not because there's some huge upside in using them. Did I miss the magic "aha" moment?
- aswanson 8y agoTemplates make for the most abusively ugly code, at a glance. The syntax interpetaion to translate to natural problem-domain thought is so brutal.
- polskibus 8y agoZero-cost abstraction. In most cases constructs in other languages either lack expressiveness (generics in C#) and/or induce runtime costs (boxing/unboxing, etc.).
- krylon 8y agoIn many (most?) cases, you do not need the performance (which is why people use C#, Java, etc.). When the performance is needed, the burden of using C++ seems like a small price to pay[0], but a lot of the time, the expressiveness offered by C# or Java, etc. is entirely sufficient. Which is why "enterprisey" software is dominated by Java or .Net, and why certain software like Autodesk Inventor 3D is written in C++. [0] Which is why browser engines, etc., are written in C++. Maybe one day Rust will replace C++, but for the time being, C++ is the top of the abstraction/performance pyramid.
- deleted 8y ago[deleted]
- bluGill 8y agoThe magic moment is when you realize that your month of pain writing something complex saves the company years of writing the simpler version by hand. In the template a fixed bug is fixed once and for all. In the other version fixing a bug in once place doesn't mean anything about anything else. Note that for the above to apply you need have a domain where there will be enough uses to make the generic version worth it. Of course, just like it is fun to write code for the IOCCC, there is some perverse fun in writing complex templates.
- namirez 8y agoTemplate metaprogramming is the most powerful feature of C++. It's true that the syntax is clunky, but since I started using templates, I've never looked back to the old style of coding. C++20 will be even more powerful by adding concepts to templates.
- hfdgiutdryg 8y agoWith regular inheritance, you affect behavior from the bottom up. With the "Curiously Recurring Template Pattern", you affect behavior from the bottom up and from the top down. To be honest, I don't know if it has value for most tasks people care about (outside of libraries, I mean). But it was fantastic for optimizing OOP-abstracted desktop applications. If you have some subset of functions that need to be called a lot, then avoiding virtual functions really helps.
- fsloth 8y agoTemplates should be applied only when the problem obviously becomes simpler by employing them. You start from the non-template version and then you start to see a pattern emerging, and at some point you realize all that compmexity can be traded for another kind of complexity in the form of templates - and sometimes that trade is worth it.
- pjmlp 8y agoBecause it is the closest that C++ has to something like Lisp macros. While it will never match the simplicity of Lisp, it is easier to get C++ accepted into most companies than Lisp. And if one has access to a C++17 compiler, many tricks can now be replaced by more developer friendly constructs.
- pcwalton 8y agoI'd argue that the C preprocessor is the closest thing C++ has to Lisp macros. Templates are fundamentally based on static types, which are obviously something Lisp doesn't have.
- Yoric 8y agoWell, the C preprocessor is a pretty far cry from the kind of power of Lisp macros. If you want something as powerful as Lisp macros (ish), there isn't much else you can use in C++ land.
- pjmlp 8y agoYou can do quite a lot with if constepxr and TMO tricks, even stuff like generation of serialization code, even if isn't as clean as a Lisp implementation would be. And who knows, maybe by C++23 some form of reflection will be finally available.
- pjmlp 8y agoThat misses the point of duck typing in C++ templates, which is one of their great features, and also the cause of those wonderful instantiation error messages. In Lisp the same error would be a runtime one.
- pcwalton 8y agoThere isn't a defensible reason for untyped templates. Generics and concepts are a better design all around. It's not only error messages that matter: scoping matters more, as the problems with templates in that regard are the reason why C++ has the ADL mess. Concepts are one of the most important features C++ needs.
- Rusky 8y agoToo bad we're stuck with concepts-lite, where templates are still untyped and the scoping is all still done the same old messy way.
- nitwit005 8y agoI'd note that the nicer constructs you see in other languages were often based on people's experiences with C++. You can pretty clearly see some syntax and ideas copied into Java/C#, but it's true elsewhere as well. It's not surprising that newer languages learn from the past. That said, almost all of those nicer variants do come with some added runtime costs. The general problem of making efficient generic code, without a bunch of boilerplate, and with a nice syntax, seems fairly difficult.
- mhh__ 8y ago> Other languages have much simpler and nicer constructs and seem to work just fine. If you mean ML/Haskell, then I agree. If you mean other languages filling the same void then I don't. Idiomatic C++ uses templates: They're objectively safer than what preceded them, for one.
- blt 8y agoThey do it because C++ tempts you to strive for perfect expressive syntax and perfect efficiency at the same time. Most languages have limited expressive power (C, Fortran, Pascal) or limited efficiency (Haskell, ML, any JVM or scripting language), so you accept their limitations and move on. C++ makes programmers become Icarus. Once they escape from the prison of C syntax, they try to fly higher, towards the goal of code that looks like Python but runs like C. They learn that template metaprogramming can help acieve this goal, so they use it. But template metaprogramming is actually the sun, melting their code into a pile of wax and feathers, sending them crashing back down into the ocean to drown.
- infinite8s 8y agoOr maybe they should just learn LISP, which gives you both.
- ur-whale 8y ago> still don't understand why people bother with template metaprogramming Speed.
- alephr 8y agometaprogramming is useful (for the same reason programming is, computers are great at transforming data. your program is data and it can be much faster to write it with this in mind) and templates are an existing, portable way to do metaprogramming. despite how bad they are designed, that they already exist and are portable puts them in a position where they can be the most cost effective way to do something
- Const-me 8y ago> you can often solve the problem with more verbose, but simpler code. Sometimes. Other times, templates are simpler. > I have the impression that people use it because they can I think people use template metaprogramming because they see it in standard library, but they don’t understand the requirements for C++ STL were very different from the requirements of the code they’re personally developing. I use C++ templates for following reasons. 1. For data structures such as custom containers. 2. For zero-overhead polymorphism. E.g. when I write algorithms that must work exactly same way with floats and doubles, or with char and wchar_t strings. Or for zero-overhead lambdas, putting them in std::function causes virtual function call overhead, lambdas are usually inlined. 3. For zero-overhead constants. There’s constexpr in modern C++, also modern compilers are good at propagating constants, but still a template argument is more reliable. When I need complex metaprogramming, I often use other tools to generate the C++ code I need. On Windows it’s usually T4 templates with C# inside them, on Linux usually python. Compared to C++ templates, both T4 and python have much better support in IDEs (visual studio / PyCharm), including debuggers.