5

After writing an answer to this question which displays the solution at compile time with an error, I wondered if it was possible to get a warning instead and finish compilation (as is actually specified in the question).

While diagnostics in general are compiler-dependant, it's pretty obvious for some code that an error will get triggered (such as accessing a non-existent member or trying to instantiate an object of incomplete type).

The same can't be said for warnings though, since these tend to differ a great deal between compilers. Even though it's reasonable to assume that warnings triggered with GCC will also get triggered with Clang, the same can not be said for Visual C++.

Question:
Which warnings, if any, will consistently get triggered on all three mentioned compilers?

/W3 on VC++ and -Wall on GCC & Clang may be assumed.


Note that this is not only useful for that question, but may be useful for triggering a warning for user-defined messages aswell.

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • 1
    Why do you use `/W3` on MSVC while using `-Wall` on GCC/Clang? MSVC supports `/W4` and `/Wall` as additional warning levels. – Cody Gray - on strike Jan 07 '12 at 12:28
  • @Cody: I think of them as the "default" setup for the compilers. I don't want to have to change the warning level to get the warning displayed. `/W3` is the actual default in Visual Studio and `-Wall` can be seen as a default too. – Xeo Jan 07 '12 at 12:32

1 Answers1

2

This should work on MSVC, GCC, and Clang:

#pragma message("hello world")

Not very useful, but still works.

These picked up warnings too:

  • unused variable
  • unused label
  • large values e.g. (1 << 128)
Pubby
  • 51,882
  • 13
  • 139
  • 180
  • Can't display any template stuff with that. :( And that doesn't really count as a warning now, does it? – Xeo Jan 07 '12 at 11:49
  • @Xeo Yeah, although it is guaranteed to work - warnings not so much. – Pubby Jan 07 '12 at 11:52