You can make all warnings being treated as such using -Wno-error . You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).
To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.
Use of the warning pragma in the function to change the state of a warning number larger than 4699 only takes effect after the end of the function. The following example shows the correct placement of a warning pragma to disable a code-generation warning message, and then to restore it.
#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop )
If you only want to suppress a warning in a single line of code (after preprocessing)[1], you can use the suppress
warning specifier:
#pragma warning(suppress: 4101)
// here goes your single line of code where the warning occurs
For a single line of code, this works the same as writing the following:
#pragma warning(push)
#pragma warning(disable: 4101)
// here goes your code where the warning occurs
#pragma warning(pop)
[1] Others have noted in comments below that if the following statement is an #include statement that the #pragma warning(suppress: 4101) statement would not effectively suppress the warning for every line in the header file. If one were intending to do that, one would need to utilize the push/disable/pop method instead.
#pragma
push/pop are often a solution for this kind of problems, but in this case why don't you just remove the unreferenced variable?
try
{
// ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
// ...
}
Use #pragma warning ( push )
, then #pragma warning ( disable )
, then put your code, then use #pragma warning ( pop )
as described here:
#pragma warning( push )
#pragma warning( disable : WarningCode)
// code with warning
#pragma warning( pop )
Example:
#pragma warning(suppress:0000) // (suppress one error in the next line)
This pragma is valid for C++ starting with Visual Studio 2005.
https://msdn.microsoft.com/en-us/library/2c8f766e(v=vs.80).aspx
The pragma is NOT valid for C# through Visual Studio 2005 through Visual Studio 2015.
Error: "Expected disable or restore".
(I guess they never got around to implementing suppress
...)
https://msdn.microsoft.com/en-us/library/441722ys(v=vs.140).aspx
C# needs a different format. It would look like this (but not work):
#pragma warning suppress 0642 // (suppress one error in the next line)
Instead of suppress
, you have to disable
and enable
:
if (condition)
#pragma warning disable 0642
; // Empty statement HERE provokes Warning: "Possible mistaken empty statement" (CS0642)
#pragma warning restore 0642
else
That is SO ugly, I think it is smarter to just re-style it:
if (condition)
{
// Do nothing (because blah blah blah).
}
else
as @rampion mentioned, if you are in clang gcc, the warnings are by name, not number, and you'll need to do:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// ..your code..
#pragma clang diagnostic pop
this info comes from here
Instead of putting it on top of the file (or even a header file), just wrap the code in question with #pragma warning (push)
, #pragma warning (disable)
and a matching #pragma warning (pop)
, as shown here.
Although there are some other options, including #pramga warning (once)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With