c++cc-preprocessor

Convert string to non string with C macro


I know that it is possible to convert something to string with macro like this:

#define STRING(s) #s

printf("%s", STRING(i am string));

But it is possible to do the opposite?

#define MyType(type) ??? 

MyType("uint16_t") myint = 100;

Solution

  • AFAIK, it is not possible using the standard C preprocessor. What you want is not part of the standard C11 (or C99) language. And neither part of C++11 or C++14 (which is a different language than C).

    But you might use some different preprocessor or some script to transform your weird source file into some C file.

    You could also perhaps customize your compiler (e.g. with a GCC plugin or a MELT extension) to add such behavior thru additional builtins or pragmas. That would be very compiler specific, and probably requires more work than what you can afford.