Both Macro expansion & Code generation have pros & cons. What's your favorite approach and why? When should we choose one over the other? Please kindly advise. Thank you!
Macro expansion can be very handy & helpful: http://dtemplatelib.sourceforge.net/table.htm
vs
While Code generation gives you plenty of nice code: http://code.google.com/p/protobuf/ http://incubator.apache.org/thrift/
For c++ I prefer either template metaprogramming or code generation over macros, but macros still have their uses.
The example you have given with dbtemplatelib could be covered with c++0x Variadic Templates, with additional benefits like type checking etc.
In C or C++, macro expansion is notoriously difficult to debug. On the other hand, writing a code generator is easier to debug because it's a separate program in itself.
However, you should be aware that this is merely a limitation of the C preprocessor. For example, in the Lisp family of languages, macro expansion is code generation, they're exactly the same thing. To write a macro, you write a program (in Lisp) to transform S-expression input into another S-expression, which is then passed to the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With