Under Habrakat, a small etude allows you to write small or large applications that contain their own code and look almost adequate.
#include <stdio.h> #define REPEAT(...) #__VA_ARGS__;__VA_ARGS__ char * ref = REPEAT( int main() { printf("#include <stdio.h>\n" "#define REPEAT(...) #__VA_ARGS__;__VA_ARGS__\n" "char * ref = REPEAT(%s)", ref); return 0; } )
As can be seen from the source, it is necessary to explicitly write off the code only from the “header”, because everything that is in the REPEAT block will be automatically sharpened and saved.
The REPEAT macro itself is organized as follows: it takes a variable number of arguments (so as not to be afraid of commas), then "string" what was passed to it, and then again repeats what it was passed to, but this time without quotes (to keep the functionality).
Here is what happens after preprocessing:
g++ -E main.cpp
char * ref = "int main()" "{" " printf(\"#include <stdio.h>\\n\"" " \"#define REPEAT(...) #__VA_ARGS__;__VA_ARGS__\\n\"" " \"char * ref = REPEAT(%s)\", ref);" " return 0;" "}"; int main() { printf("#include <stdio.h>\n" "#define REPEAT(...) #__VA_ARGS__;__VA_ARGS__\n" "char * ref = REPEAT(%s)", ref); return 0; }
Frankly, I changed the code and brought it into a readable form, because the preprocessor almost sculpts everything into one line. However, apart from hyphenation and extra quotes, everything is in its place.
After launch, the functionally identical source will be displayed in stdout. The design will only faint a little.
PS #include can be entered into the REPEAT block, then it will be possible not to write it explicitly in the string.