Escaping a # symbol in a #define macro?

2020-01-24 13:04发布

问题:

Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)

For example, I want to do something like this:

#define MACRO(name) #include "name##foo"

And use it thus:

MACRO(Test)

Which will expand to:

#include "Testfoo"

The humble # sign is causing the preprocessor to barf. MinGW gives me the following error:

'#' is not followed by a macro parameter

I guess I need to escape the # sign but I don't if this is even possible.

Yes, macros are indeed evil...

回答1:

As far as I remember you cannot use another preprocessor directive in define.



回答2:

It is possible to insert a hash token into the preprocessed token stream. You can do it as follows:

#define MACRO(hash, name) hash include name
MACRO(#,"hello")

—expands to:

# include "hello"

However, the standard explicitly rules out any further analysis of such line for the existence of preprocessing directives [cpp.rescan]:

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one.



回答3:

The problem isn't actually getting a # symbol in the output of your preprocessor.

Apparently you want the preprocessor to reparse your file, to deal with newly created #include directives as part of macro expansion. It doesn't work that way. If a line starts with #, it's an instruction for the preprocessor and interpreted. If a line doesn't start with #, it's only subject to preprocessor transformation including macro substitution. This is a once-per-line test.

MACRO(Test)

does not start with #. Therefore it is not interpreted as a preprocessor directive; instead it's subject to macro replacement rules.



回答4:

This is because the # has special meaning when used in a macro.

#  means quote the following token (which should be a macro parameter name)
## means concatenate the preceding and following tokens.

In your situation the # is not followed by a proper token. So in your situation we need to go through a level of indirection:

#define     QUOTE(name)     #name
#define     TEST(name)      QUOTE(name ## foo)

#include TEST(scot)


回答5:

You can't do that. Preprocessor directives are recognized before macro expansion; if the macro expands into something that looks like a preprocessor directive, that directive will not be recognized. The best you can do is create a macro for the file name:

#define MACRO(name) "name##foo"
...
#include MACRO(Test)


回答6:

This might work (it works for regular #define macros with no parameters, but I haven't tested it with macros with parameters).

#define MACRO(name) <name##foo>
#include MACRO(Test)


回答7:

#define HASH_SIGN #
BOOST_PP_CAT(HASH_SIGN, include)


回答8:

#define PARAM_NAME Param
#define GETNAME_(a) #a
#define GETNAME(a) GETNAME_(a)

int Param;
printf("%s = %i\n", GETNAME(PARAM_NAME), PARAM_NAME);