Metaprogramming, is it a solution?

Why Metaprogramming?

Benefits:

  • We could do the computation at runtime instead of compile time. By doing as much work as possible before the resulting program starts, we get faster programs. The result of the computation can interact more deeply with the target language. This is possible because the actions are processed during grammar compilation and passed to the target C/C++ compiler.
  • Once the metaprogram is written, its benefits of convenience can be spread across a community of other programmers. (For a metaprogram that will only be used once, different alternatives might be more efficient)
  • Regardless of how many times it’s used, a metaprogram enables its user to write more expressive code, because the result can be specified in a form that corresponds to programmer’s mental model.
  • The resulting program is more likely to be correct and maintanable.

Why C++ Metaprogramming?

  • User can enter the domain language directly, without learning a foreign syntax or interrupting the flow of the code
  • Interfacing metaprograms with other code, especially other metaprograms, becomes much smoother
  • No additional build step is required
  • Metaprogramming moves the computation required for expressivity and correctness from runtime to compile time

When Metaprogramming?

  • The code needs to be expressed in terms of the abstractions of the problem domain
  • When it is needed to write a great deal of boilerplate implementation code
  • When it is needed to take the advantage of valuable properties of generic programming in C++ such as static type checking and behaviour customization, without loss of effiency
  • The code needs to be written in C++ language, without an external tool or custom source code generator

[summarized from this book]

Conclusion:

  • For my case, I don’t have to understand about metaprogramming to utilize the templates (which is good 😉 )
  • This writing about metaprogramming really helps me to move on without worrying about less prior tasks. Thx buddy 😉
Advertisements