2

The following doesn't compile:

#define SUPPRESS(w) _Pragma("GCC diagnostic ignored " ## w)

SUPPRESS("-Wuseless-cast")

int main() {
    int a = (int)4;
    return a;
}

Here's the error:

error: pasting ""GCC diagnostic ignored "" and ""-Wuseless-cast"" does not give a valid preprocessing token

How can I get it to work?

onqtam
  • 4,356
  • 2
  • 28
  • 50
  • Depending on which [phase of translation](http://en.cppreference.com/w/cpp/language/translation_phases) the `_Pragma` directive is handled, you could perhaps rely on the standard consecutive-literal-string concatenation. That means if you have two literal string constants with only whitespace (or comments) between them, then they will be automatically concatenated into a single string. If so then you don't need the preprocessor concatenation operator. You might want to try that. – Some programmer dude Sep 01 '17 at 11:51
  • @Someprogrammerdude like ```_Pragma("GCC diagnostic ignored " w)```? doesn't work. Also you can experiment with the online compiler link I provided. – onqtam Sep 01 '17 at 11:53

1 Answers1

3

The thing is that _Pragma wants to have an escaped string-literal like so

_Pragma("GCC diagnostic ignored \"-Wuseless-cast\"")

So the trick is to add another layer of stringyfication between the call of SUPPRESS and the call of _Pragma like below

#define xSUPPRESS(w) _Pragma(#w)
#define SUPPRESS(w) xSUPPRESS(GCC diagnostic ignored w)

SUPPRESS("-Wuseless-cast")

See it here in action.

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34