C++ Generating code with macro
In order to avoid repetition of code, which means a big No to copy-and-paste code, I need to put similar code into a macro, or a function. Considering this, macro wins over the choice of using C++ function. This is not something new, absolutely not but it was so clumsy for my first and second attempts. Therefore, this post summarizes hopefully in a clear manner what is needed to achieve “code generation”.
Everything you should know about C/C++ marco is listed here Marcos. In particular, I refer to the subsection Marcos - Concatenation.
In this post, I will describe here some simple code with generating some function declaration. The first thing first, I create Test.hpp file to host macros. Line 2-4 defines a dictionary that contains
The code is self-explanatory I think but Line 8 and Line 13 use ** ## name** to concatenate the left and the right tokens with the argument name as token.
The last piece of this puzzle is the Test.cpp that includes Test.hpp and use the generated functions.
Notes By the way, there is a link which describes how to wrap your code block in Wordpress here. And of course, you should enable writing in Markdown for your site as described here