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.
Concepts are one of the most important features C++ needs.
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.
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.
That you can use templates for something else is almost entirely useful only if you want to win something like the IOCCC.
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.
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.
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.
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.
And who knows, maybe by C++23 some form of reflection will be finally available.
In Lisp the same error would be a runtime one.
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.
Speed.