You can do this by combining two functions. One of them is the "structure", as a result of which the macro is converted to a string by prefixing it with #
. (This is related to, but different from the `` token-pasting '' ##
operator, which you are obviously already familiar with.) Another fact is that C ++, when specifying multiple string literals in a string, will combine them into one line. For example, "a" "b" "c"
equivalent to "abc"
. I donβt quite understand how exactly your macro should be defined, so I canβt show you what exactly to type, but a full explanation and some good working examples can be found at http://gcc.gnu.org/onlinedocs/cpp/Stringification. html
Edited to add a simple example, as requested by Kleist. This program:
#include <stdio.h> #define PRINT_WHAT_THE_NAME_OF_THE_TYPE_IS(TYPE) \ printf("%s\n", "'" #TYPE "' is the name of the type.") int main() { PRINT_WHAT_THE_NAME_OF_THE_TYPE_IS(Mr. John Q. Type); return 0; }
will print this:
'Mr. John Q. Type' is the name of the type.
(This will work either in C or C ++. The reason I wrote C-ishly is because, in my experience, these kinds of preprocessing tricks are more common in C code than in real C ++ code, but if you want to use std::cout <<
instead of printf
, you absolutely could.)
ruakh source share