Reputation: 170539
Visual C++ features #pragma warning
that among other things allows to change the warning level of a specific warning. Say warning X has level 4 by default, then after
#pragma warning( 3, X )
it will have level 3 from that point.
I understand why I would temporarily disable a warning, turn a warning into an error or turn on a waring that is off by default. But why would I change a warning level?
I mean warning level of a specific warning works together with the compiler "warning level" option /W
, so I currently can't imagine a setup in which I would want to change a level of an individual warning since the warning being emitted or not will depend on the project settings.
What are real-life examples of when I really need to change a warning level of a specific warning in C++?
Upvotes: 4
Views: 506
Reputation: 31
An example of where I use this is error C4996 where I move it from being a level 3 warning to level 4. For instance, the VS compiler warns that sprintf may be unsafe and suggests you use the non-portable sprintf_s function instead. You can get lots of these warnings and rather than wading through all of these to find the real errors, I prefer to just prevent them being issued.
I know I could define a macro for sprintf (and loads of other functions) instead, which would maintain portability, and strictly that should be done, but in a large project, tracking down and fixing all these warnings is some effort, for probably very little return.
Upvotes: 2
Reputation: 36092
In some cases when you are working with legacy code and due a newer compiler becoming more sensitive you may want to turn down the warning level for those modules to avoid thousands of warnings. Also in some cases when you interact with generated code you would like to turn down the warning level. e.g. ANTLR generates C-code that you treat as a black box so you don't want warnings then.
Upvotes: 1
Reputation: 6831
It is much easier to take the one or two level 4 warnings you want detected and use the pragma to bring them into level 3 than it is to use /W4
and disable (/Wd####
)the all the warnings you don't care about.
Upvotes: 4
Reputation: 18974
When you want to run on level 3 or 4, but you want to see/not see a specific message, you can change its level. Sure, the pragma might have no effect if the warning level isn't what you think, but that's life with pragmas.
Upvotes: 4
Reputation: 2939
My experience is to treat warnings and errors and strive to produce code without warnings. I know that this might seem like a stupid or overly strict approach, but it really does pay off in the long run. Trust me!
Upvotes: -2