To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at

If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

So, You Want to Suppress This Warning i…

So, You Want to Suppress This Warning in Visual C++...

Aug 21 2013

A common situation: you've just written a piece of spotless code, but Visual C++ emits a warning on it. Rewriting the code a bit usually helps get rid of the warning - usually, but not always. Then you only have one way out, which is to suppress it. Let's see what capabilities Visual C++ provides for that purpose, and what mistakes are common among programmers when using them.

This is a translation of an article written by Dmitry Meshcheryakov, an ABBYY employee and first published here: "ABBYY blog. So, You Want to Suppress This Warning in Visual C++...". Translation done and published with permission of the copyright holder.

We decided to publish this article in our knowledge base because it gives a very clear explanation of the essence of PVS-Studio's V665 diagnostic.

The most obvious method, is to suppress the warning in the project settings at the project level. It works, but not very well. First, the warning will be suppressed in the whole project, including all the header files the project includes. Second, copying the code into another project will bring the warning back. It will inevitably happen if header files (which, for example, contain template implementations) contain code you need to include (#include) into every project using them.

Another method is to suppress the warning in the project settings at the file level. This method is even worse than the previous one. First, the warning will be suppressed for the whole translation unit, i.e. in this particular file, and all the header files it includes. Second, you'll get the same troubles with code copying. Third, once your project includes more than a few files, the probability of losing this setting during project conversion for a new Visual C++ version gets just about 1.

The last thing you can do is use #pragma warning. Programmers usually use it like this:

#pragma warning (disable: 9000)
// code triggering C9000 warning
#pragma warning (default: 9000)

... and feel very pleased with themselves: the warning suppressed, the code ready, the warning restored - take profit.

It is in fact an epic FAIL. Now it's time for you to read attentively (yes, read attentively - instead of copying-and-pasting code from anywhere you like), the description of #pragma warning (default). It says that this construct

  • Sets the warning at the default severity level and
  • Turns on the warning.

Levels first. In Visual C++, each warning is assigned a number from 1 to 4 to specify the warning's severity level. Warnings of Level 1 are considered to be the most severe, and severity is thought to drop as the level number grows. Each warning has a default severity level. The construct

#pragma warning(Level: Warning)

sets the warning to the specified level and turns it on, i.e. the level is not nailed to the warning - you can change it if you wish.

The compiler has a setting (Warning Level) that specifies the threshold, such that only warnings with a level not higher that this threshold are emitted. When this parameter is set at A, a warning for a particular code line is shown only if it is permitted for this line, and its level is A or lower.

Besides, a number of Visual C++ warnings are disabled by default, for they are generated even in the safest code, and everyone is sick and tired of them. Anyone who is about to protest against the very idea of local suppression of concrete warnings should realize and feel this fact deeply before protesting.

Let's see how the FAIL with #pragma warning (default) manifests itself.

FAIL 1. The warning C9001 is disabled by default. The code in the header file uses #pragma warning(default:9001) to "restore" the warning suppressed in a small code piece.

Why would it do so, when the warning is already disabled? The list of Visual C++ warnings disabled by default changes from version to version, as more and more warnings get added into it. If code was initially written for Visual C++ 7, where C9001 was enabled by default, and is now compiled in Visual C++ 10 where it is disabled, then this construct is meaningless, and/or it could be a piece of legacy code.

It will result in #pragma warning(default) force-enabling the warning disabled by default.

FAIL 2. The warning C9002 has a default Level 3, while the project is compiled with a Level 2 threshold, i.e. with the instruction "to show warnings of Level 2 and lower". Developers were thinking it over for a long time, and finally concluded that C9002 was actually serious enough to promote it to Level 2, that is, to force-increase its severity. Correspondingly, each project includes a standard header file with the instruction #pragma warning(2:9002), which then gets into all the translation units.

A bit further in the text of a translation unit, there is the instruction #pragma warning(default:9002), which sets the warning level back to 3, so the warning is not generated during compilation with a Level 2 threshold. This warning reported a critical defect, by the way. Smile and wave. It can work backwards as well. For example, a warning was given a higher level (3 instead of 2), so that it is not shown in projects compiled with a Level 2 threshold (i.e. its severity was lowered), but #pragma warning(default) sets the level back to 2, and you'll start seeing it again.

FAIL 3. The warning C9003 is by default enabled, yet it is implemented in such a bad way, that nobody can recall a case when it was helpful. Developers dare to suppress it everywhere by using #pragma warning(disable:9003) in the common header file. #pragma warning(default:9003) occurs further in the translation unit, and enables the warning.

What is especially nice, is that these situations always occur at the right moment - when switching from one compiler version to another, or when trying to include a heap of third-party code - which is already troublesome enough - into the project; while the FAIL 2 scenario simply suppresses the warning, which may result in missing a severe error in the code, and helping the user to win a Darwin Award.

Warnings must be suppressed in the following way:

#pragma warning(push)
// fine vacancies:
#pragma warning(disable:9000)
// code with warning C9000
#pragma warning(pop)

The first instruction saves the current state of warning settings, the second disables the necessary warning, and the third restores the previously saved state. The state is restored completely: you get back all the changes made at higher levels, while warnings disabled by default are not enabled.

It looks scary, but you'll do anything not to miss that very billion-dollar bug, won't you?

Popular related articles
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →