6

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;
y30
  • 722
  • 7
  • 17
  • 2
    Smells awefuly like an [XY problem](http://xyproblem.info). You really should **improve your question** (by editing it) to motivate it. I cannot imagine a case where doing what you want is actually useful. – Basile Starynkevitch Dec 22 '16 at 11:22
  • But how does it know how to convert it from a string to any arbitrary type? Or will it always be an integer type of some precision? What about floating point? – Jerry Jeremiah Dec 22 '16 at 11:24
  • 1
    Preprocessor can join tokens together, but it can't break a token, so short answer is NO. However you can create something that might do something similar, but it depends on use case and your problem, but I don't think it is a thing you really need. – ST3 Dec 22 '16 at 11:26

1 Answers1

5

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547