C Macro - no error was declared in this area

#define SET_NONEMPTY(x) { const NString& var = r->hdrs->get_##x(); \ if (!var.empty()) { \ set_##x_for_serving(r->request, var.data(), var.length()); \ } \ } 

The above macro tries to set the query member if it is not empty, but I get the following error: 'set_x_for_serving' was not declared in this area while I use this macro.

What is wrong with this macro?

+6
source share
2 answers

You need a token labeling operator on either side of x to get it replaced correctly.

 #define SET_NONEMPTY(x) { const NString& var = r->hdrs->get_##x(); \ if (!var.empty()) { \ set_##x##_for_serving(r->request, var.data(), var.length()); \ } \ } 
+9
source

It looks like inside the macro message SET_NONEMPTY(foobar) you expect set_##x_for_serving to expand to set_foobar_for_serving .

Is it correct?

If so, the phrase x_for_serving represents a single token, and x will not be considered by the preprocessor as an element for replacement.

I think you want set_##x##_for_serving instead:

 #define SET_NONEMPTY(x) { const NString& var = r->hdrs->get_##x(); \ if (!var.empty()) { \ set_##x##_for_serving(r->request, var.data(), var.length()); \ } \ } 
+5
source

Source: https://habr.com/ru/post/955405/


All Articles