What's the use of metaprogramming?

后端 未结 11 693
清酒与你
清酒与你 2020-12-12 11:12

I\'ve read:

  • Wikipedia
  • Code Generation vs. Metaprogramming
  • The art of Metaprogramming
  • Metaprogramming at c2.com

and I

11条回答
  •  无人及你
    2020-12-12 11:33

    My recent (last 6 months) concrete example of code generation:

    1. I have an SQL Plus script that generates and then executes other SQL Plus scripts. The generates script runs queries against some tables that have time-stamp fields, and when I designed the script, it was impossible to know what time window to select. So, the main script does its work, and figures out what time ranges need to be in the sub scripts. Then it generates the subscripts by writing their code to file (and substituting placeholders for the actual start and end times). Finally it executes the subscript(s). I've used this trick for a few situations now (though often more complicated than this one) where the structure of the substeps depends on results of earlier steps.

    2. I once got a spreadsheet mapping elements from an XSD to table columns in a database. It was possible to generate XSL snippets and complete queries from the spreadsheet using macros and VBA. These snippets and queries were copied and pasted (mostly as-is with no neede changes) into the system that executed them and processed the results. Not a pretty solution but it certainly made a very tedious job a lot less tedious, and the code that resulted was probably a lot more consistent-looking than if I had spent a week or two writing it all by hand.

    SO list of examples of metaprogramming: What are the coolest examples of metaprogramming that you've seen in C++?

提交回复
热议问题