Others might have some comments on other limitations, and what could go wrong :). But it can go inside another pseudo-macro :). You just can't use the stuff it creates as the input into another macro, like you can with normal macros, since you can't stick the include inside a macro. It can obviously be extended to have as many "parameters" as needed, and you can run whatever preprocessor commands you like in there, plus generate actual code. I haven't used this trick extensively, but it gets my job done. These includes could even be in the middle of codes blocks you want to repeat (with FOO altered), as the answer by Bing Jian requests: macro definition containing #include directive Here we automate the including of groups of similarly named files #include INCLUDE_FILE(head1,tail1.hpp) // expands to #head1FOOtail1.hpp #define INCLUDE_FILE(HEAD,TAIL) STRINGIFY( CAT_3(HEAD,FOO,TAIL) ) FOO fulfils the role of "parameter" in this pseudo-macro. in order to alter which files it will in turn #include. Note especially FOO, which we can #define before #include-ing this file, (cons (quote a) (append (list (quote b) (quote c)) (list (+ 1 2)))). You can partially automate their inclusion by doing something like this: The traditional Lisp macro expansion facility inhibits several important forms of. It is tedious to write them all out, or perhaps even they are auto-generated. When pasted, they make a longer identifier. Usually both will be identifiers, or one will be an identifier and the other a preprocessing number. It doesn't behave exactly like a macro would, but it can achieve some pretty macro-like results, since #include basically just dumps the contents of one file into another.įor example, consider including lots of similarly named headers that come in groups. When a macro is expanded, the two tokens on either side of each ‘ ’ operator are combined into a single token, which then replaces the ‘ ’ and the two original tokens in the macro expansion. So, you can stick a block of code you want to mutate into a file, thinking of it like a macro definition (with pieces that can be altered by other macros), and then #include this pseudo-macro file in various places (make sure it has no include guards!). Realise that preprocessor directives won't do anything inside a macro, however they WILL do something in a file. However, you can make the preprocessor do basically the same thing with a gnarly trick I found myself using recently. So like the others say, no, you can't have #include statements inside a macro, since the preprocessor only does one pass.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |