Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to remove quotes in a C macro?

Suppose I want to un-stringify the macro argument which should transform "text" to text.

#define UN_STRINGIFY(x) /* some macro magic here */

Now calling this macro will remove "" from its argument

UN_STRINGIFY("text") // results in ----> text

This would be the opposite of macro stringification:

#define STRINGIFY(x) #x

Is this possible, or am I playing with macro evilness?

like image 700
syvex Avatar asked Apr 18 '12 13:04

syvex


People also ask

How do you remove quotation marks in C++?

Removing double quotes from string in C++ We can do this by using erase() function. Basically, what is the work of erase() function? It removes the elements from a vector or a string in some range.

What are the function of and## in the replacement sequence in function macros?

# and ## operatorsAll leading and trailing whitespace is removed, and any sequence of whitespace in the middle of the text (but not inside embedded string literals) is collapsed to a single space.

How to define macros in Cpp?

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).


2 Answers

It's not possible. And that's probably a good thing: If you pass a string you assume you can put pretty much everything in it. Un-stringifying it would suddenly result in the compiler actually caring about the contents of that string.

like image 192
ThiefMaster Avatar answered Sep 19 '22 22:09

ThiefMaster


I came to this question already several times and still I can't get it into my head. Consider this example:

#define FOO(x) x

// FOO( destringify("string x;"))   // no way

auto f = "string x;";
FOO(string x;)                      // hm whats the problem?

To me it seems obvious that it should be possible to remove the quotes. I mean string x; is nothing else than "string x;" without the quotes. The thing is: It is just not possible. I don't think there is a technical reason for that, and one can only speculate why there is no method to do it.

However, I managed to convince myself by remembering that basically all the preprocessor does is textual replacement, hence why would you want to "de-textify" something when on the level of the preprocessor anyhow everything is just text. Just do it the other way around. When I change the above example to this:

#define FOO(x) x
#define STR(x) STRSTR(x)
#define STRSTR(x) #x

#define STR_X string x;

auto f = STR(STR_X)
FOO(STR_X)

Then there is no need to de-stringify. And if you ever find yourself in the situation that you want to de-stringify a string via a macro that is not known before compile-time, then your are on the wrong track anyhow ;).

like image 45
463035818_is_not_a_number Avatar answered Sep 20 '22 22:09

463035818_is_not_a_number