Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is metaprogramming important? Isn't scripted code-generation easier?

I last used C++ extensively around the time metaprogramming became popular, and I'm just now coming back. I did a heap of scripted C++ code generation in the past. Although I see metaprogramming is powerful, does it truly offer anything over scripted code generation? One advantage I see is scripting isn't "round trip" where metaprogramming is inline with the source code, so there is always synchronicity between the non-runtime code and the runtime code.

like image 431
Greencpp Avatar asked Oct 26 '25 15:10

Greencpp


1 Answers

You've already gotten a couple of answers mentioning type safety, and that is a good reason -- but it's definitely not the only one. The big problem with using scripts to generate code is that it distributes difficultly throughout the build cycle and all the programmers using it. That is to say, even given a working script, the programmer using the script has to be reasonably aware of its existence. The build cycle has to be written to be aware of the script, and debugging with script-generated code tends to be relatively difficult as well, because you're debugging code neither you nor any other person actually wrote. Script generated code is rarely written quite like a normal person would write the same thing, making it doubly difficult to debug.

Metaprogramming tends to concentrate the difficulty -- but even though it can be difficult to write, using it is usually quite straightforward. As far as debugging goes, it depends: template code can be written to statically check its arguments at compile time and (at least try to) produce meaningful error messages when/if it's used incorrectly. Of course, some doesn't, just like some functions accept their inputs and try to use them without checking that the inputs that were supplied meet its requirements.

At the same time, once template code is written, using it is generally quite straightforward. In fact, since templates normally have to be in headers, complex metaprogramming tends to be about the easiest code to incorporate into an end product. Normally all you have to do is include the right header; you don't even have to specify a library to the linker.

This tends to fit better with the real distribution of talent among programmers. A few programmers are dramatically better than the rest. With TMP, you have a much better chance of letting a few really superb programmers write the complex TMP code, and make life dramatically easier for all the average programmers. With scripted code generation, you're basically expecting everybody involved to have nearly the same level of talent. Using the script, writing other code that integrates with the script-generated code, etc., are all about equally difficult, so you basically need all your programmers to be quite good, and can't do nearly as much to leverage a few good programmers to make life easier for everybody else.

like image 89
Jerry Coffin Avatar answered Oct 28 '25 05:10

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!