1

I am wondering if there is anyway to stringify a macro expanision that is another macro before it gets completley expanded. Easier to show in code:

#define A_MACRO 0
#define ANOTHER_MACRO A_MACRO

I want to expand ANOTHER_MACRO into the string "A_MACRO"

Double stringification does not work, it stringifies ANOTHER_MACRO into 0. I have searched and cannot find an answer and played around with macros in a test application, but I have had no luck. Is it possible?

Edit: I do not have the ability to change either macro. A_MACRO has a descriptive name that I would like to use, and we "point" to it with another macro that follows a standard name and I can grab it with a file parser. I was hoping I could just write a stringification macro to get that name, but I think I will have to find a way to latch onto it with my file parser, thanks everyone for your help.

  • 1
    `#define ANOTHER_MACRO "A_MACRO"` ? Explain why it is not what you are looking for. – Eugene Sh. Jul 06 '22 at 14:52
  • Do you mean you want some macro `F` defined such that `F(ANOTHER_MACRO)` will be replaced by `"A_MACRO"`? – Eric Postpischil Jul 06 '22 at 14:53
  • start with `#undef A_MACRO` – KamilCuk Jul 06 '22 at 14:55
  • @Eugene, the macro's are defined in another file that is automatically generated. A_MACRO would be a non-standard description. I am parsing the file and finding the ANOTHER_MACRO macro that follows a standard description that I can grab onto – tyler_30130 Jul 06 '22 at 15:04

1 Answers1

0

No, there isn't. You can either not expand the macro argument, or you can fully expand it.

There may be a solution to whatever the underlying problem is, but this is a dead end.

rici
  • 234,347
  • 28
  • 237
  • 341
  • That's what it feels like, thanks. – tyler_30130 Jul 06 '22 at 15:12
  • @tyler_30130: if you explain the real problem, someone might be able to help you find a solution. But don't do that in a comment. Post a new question. – rici Jul 06 '22 at 15:14
  • https://stackoverflow.com/a/11694231/11683? – GSerg Jul 06 '22 at 15:18
  • @gserg: if you can alter the definition of the macro being expanded, there are lots of possibilities. Using a function-like macro might be a way of restructuring the problem. But if the macro definitions are a given (eg. they are in an external header which cannot be modified) then sime other solution is necessary. – rici Jul 06 '22 at 15:23