开发者

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

开发者 https://www.devze.com 2023-01-26 00:44 出处:网络
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 i开发者_JAVA技巧n the past.Although I see metapro

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 i开发者_JAVA技巧n 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.


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.


From wikipedia:

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data[...]

So script driven code generation is metaprogramming, if you wish to split hairs.

The advantage of doing C++ metaprogramming using the built-in feature of templates is that you get the benefit of C++ strong typing system.


My opinion?

the thing about meta programming is that it is type-safe. Macros and auto-generated code all not aware of the language rules.


does [metaprogramming] truly offer anything over scripted code generation?

Take a look at the following metaprogram of mine. It overloads operator<< for any array of Ts, except if T is a char type. (Without that precaution, output of string literals would not work anymore.)

template <typename T, size_t n>
typename disable_if<
    is_same<typename remove_const<T>::type, char>::value,
    std::ostream&
>::type
operator<<(std::ostream& os, T (&array)[n])
{
    return print_range(os, array + 0, array + n);
}

So how would you "scriptify" this metaprogram? Generate real code for every possible type T? That is not feasible, because you do not know at "scripting time" which types will exist at compile time.

0

精彩评论

暂无评论...
验证码 换一张
取 消