I was trying to create a small define to work around this bug in QITABENT and I noticed peculiar behavior of the #pragma warning (disable: ...) statement.
In the following code the define QITABENT generates warning C4838
static const QITAB qit[] =
{
QITABENT(MediaPlayerCallback, IMFPMediaPlayerCallback)
};
I can easily suppress this warning, this works:
#pragma warning( push )
#pragma warning( disable: 4838 )
static const QITAB qit[] =
{
QITABENT(MediaPlayerCallback, IMFPMediaPlayerCallback)
//{ 0 },
};
return QISearch(this, qit, riid, ppv);
#pragma warning( pop )
Now I wanted to make a define QITABENTEX which automatically suppresses the warning generated by QITABENT. But it seems impossible because when I write the following code the warning C4838 is not suppressed.
static const QITAB qit[] =
{
#pragma warning( push )
#pragma warning( disable: 4838 )
QITABENT(MediaPlayerCallback, IMFPMediaPlayerCallback)
#pragma warning( pop )
//{ 0 },
};
How does the compiler interpret this code in such a way that the warning is not suppressed?
This probably has to with when the definition for QITABENT is fully resolved.
(Note that I'm not really interested in making the above code work, I'm just really curious how its interpreted by the compiler)
Addendum:
Regarding the close vote and clarification: I got into a discussion with someone giving a shotgun/link-only answer, presumably only reading the question halfway through (since the question explained how to use #pragma in a macro which is not what I'm asking) now that answer got (self) deleted and I got a close vote for being unclear. So let me reiterate my intentions with this question:
This question is not about finding a define to suppress this warning
This question is not about how to suppress a warning in VC++ in general
This question is about trying to understand what happens with the three lines of suppression code in the last code sample. Why do they not have effect in that exact position (in an array initialization) but do have effect outside the array initialization? This probably boils down to answering how and when the pragma statements and macross are resolved.
The initializer list terminates with the closing curly bracket }, and that is where warning is generated.
Try this:
static const QITAB qit[] =
{
QITABENT(Derived, Base)
#pragma warning( push )
#pragma warning( disable: 4365 )
}
#pragma warning( pop )
;
[edited]
Per Mr.C64's comment below, corrected the order of parameters in QITABENT(Derived, Base).
Related
I am beginning to learn C++ and read that increasing the warning level is a good idea to be more careful in writing your code. I am using Visual Studio 2015 (community version)
However, after doing so, I receive hundreds of errors (edit: WARNINGS treated as errors, sorry) even in basic programs. These are all coming from the math header, and they say that "unreferenced inline function has been removed". Seems like every method I don't use is causing a warning.
Why is this happening, and how can I fix it?
With Visual C++ you should use /W4, not /Wall which is impractical. If warning level 4 still produces sillywarnings, e.g. from standard headers, then consider using my old MSVC "no sillywarnings" header, or a suitable extension of it. Preferably then via a forced include compiler option.
For working with Visual C++ in the command line I generally use the following environment variable:
CL=/nologo /EHsc /GR /W4 /FI "iso646.h"
Apparently there is now automatic suppression of warnings from system headers, so it's not a problem.
Please note that while the holy C++ standard does not differentiate between different kinds of diagnostics, as a de facto standard C++ compilers and linkers differentiate between
Errors:
An error diagnostic means that no executable (or object file) is produced.
Warnings:
A warning diagnostic means that something was likely to be wrong, or at least was somewhat questionable, but the compiler or linker or other tool carried on assuming you knew what you were doing.
Relevant MSVC-specific headers you can force-include (option /FI), including a more up to date version of the no sillywarnings header:
msvc_less_errors_as_warnings_please.hpp
#pragma once
// p/cppx/core_language_support/compiler_specific/msvc_less_errors_as_warnings_please.hpp
// Copyright © Alf P. Steinbach 2015. Boost Software License 1.0.
#ifndef _MSC_VER
# error This file is specific to the MSVC (Microsoft Visual C++) compiler.
#endif
#pragma warning( error: 4566 ) // Character ... cannot be represented -- is error.
#pragma warning( error: 4627 ) // Source code has been ignored – is error.
msvc_more_warnings_please.hpp
#pragma once
// p/cppx/core_language_support/compiler_specific/msvc_more_warnings_please.hpp
// Copyright © Alf P. Steinbach 2015. Boost Software License 1.0.
#ifndef _MSC_VER
# error This file is specific to the MSVC (Microsoft Visual C++) compiler.
#endif
#pragma warning( push, 4 ) // Warning level 4 (max). MSVC /Wall is impractical.
msvc_no_sillywarnings_please.hpp
#pragma once
// p/cppx/core_language_support/compiler_specific/msvc_no_sillywarnings_please.hpp
// Copyright © Alf P. Steinbach 2010 – 2015. Boost Software License 1.0.
#ifndef _MSC_VER
# error This file is specific to the MSVC (Microsoft Visual C++) compiler.
#endif
#ifndef CPPX_ALLOW_WP64
# // The /Wp64 option generates spurious warnings when a __w64 type argument selects
# // a correct overload with non-__w64 formal argument type, i.e. for <<. In newer
# // versions of MSVC this option is deprecated. It Really Annoyed a lot of people!
# ifdef _Wp64
# error Do not use the /Wp64 option: use a 64-bit compiler to detect 64-bit portability issues.
# endif
#endif
// The following are real warnings but are generated by almost all MS headers, including
// standard library headers, so it's impractical to leave them on.
#pragma warning( disable: 4619 ) // there is no warning number 'XXXX'
#pragma warning( disable: 4668 ) // XXX is not defined as a preprocessor macro
// The following are pure sillywarnings:
#pragma warning( disable: 4061 ) // enum value is not *explicitly* handled in switch
#pragma warning( disable: 4063 ) // case 'nn' is not a valid value for switch of enum 'Name'
#pragma warning( disable: 4099 ) // first seen using 'struct' now seen using 'class'
#pragma warning( disable: 4127 ) // conditional expression is constant
#pragma warning( disable: 4180 ) // qualifier applied to function type has no meaning
#pragma warning( disable: 4217 ) // member template isn't copy constructor
#pragma warning( disable: 4250 ) // inherits (implements) some member via dominance
#pragma warning( disable: 4251 ) // needs to have dll-interface to be used by clients
#pragma warning( disable: 4275 ) // exported class derived from non-exported class
#pragma warning( disable: 4347 ) // "behavior change", function called instead of template
#pragma warning( disable: 4355 ) // "'this': used in member initializer list
#pragma warning( disable: 4373 ) // override when arg types differ by const/volatile qualifiers
#pragma warning( disable: 4428 ) // MSVC 9: universal-character-name encountered in source
#pragma warning( disable: 4459 ) // local declaration hides global declaration
#pragma warning( disable: 4505 ) // unreferenced function has been removed
#pragma warning( disable: 4510 ) // default constructor could not be generated
#pragma warning( disable: 4511 ) // copy constructor could not be generated
#pragma warning( disable: 4512 ) // assignment operator could not be generated
#pragma warning( disable: 4513 ) // destructor could not be generated
#pragma warning( disable: 4610 ) // can never be instantiated user defined constructor required
#pragma warning( disable: 4623 ) // default constructor could not be generated
#pragma warning( disable: 4624 ) // destructor could not be generated
#pragma warning( disable: 4625 ) // copy constructor could not be generated
#pragma warning( disable: 4626 ) // assignment operator could not be generated
#pragma warning( disable: 4640 ) // a local static object is not thread-safe
#pragma warning( disable: 4646 ) // noreturn function should have a void return type
#pragma warning( disable: 4661 ) // a member of the template class is not defined.
#pragma warning( disable: 4670 ) // a base class of an exception class is inaccessible for catch
#pragma warning( disable: 4672 ) // a base class of an exception class is ambiguous for catch
#pragma warning( disable: 4673 ) // a base class of an exception class is inaccessible for catch
#pragma warning( disable: 4675 ) // resolved overload was found by argument-dependent lookup
#pragma warning( disable: 4702 ) // unreachable code, e.g. in <list> header.
#pragma warning( disable: 4710 ) // call was not inlined
#pragma warning( disable: 4711 ) // call was inlined
#pragma warning( disable: 4820 ) // some padding was added
#pragma warning( disable: 4917 ) // a GUID can only be associated with a class, interface or namespace
#pragma warning( disable: 4996 ) // MSVC 9: a C stdlib function has been "deprecated" (says MS)
Is there a way to disable just a single warning line in a cpp file with visual studio?
For example, if I catch an exception and don't handle it, I get error 4101 (unreferenced local variable). Is there a way to ignore this just in that function, but otherwise report it in the compilation unit? At the moment, I put #pragma warning (disable : 4101) at the top of the file, but that obviously just turns it off for the whole unit.
#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
{
// ...
}
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
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 )
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
One may also use UNREFERENCED_PARAMETER defined in WinNT.H. The definition is just:
#define UNREFERENCED_PARAMETER(P) (P)
And use it like:
void OnMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
}
Why would you use it, you might argue that you can just omit the variable name itself. Well, there are cases (different project configuration, Debug/Release builds) where the variable might actually be used. In another configuration that variable stands unused (and hence the warning).
Some static code analysis may still give warning for this non-nonsensical statement (wParam;). In that case, you mayuse DBG_UNREFERENCED_PARAMETER which is same as UNREFERENCED_PARAMETER in debug builds, and does P=P in release build.
#define DBG_UNREFERENCED_PARAMETER(P) (P) = (P)
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).
This question comes up as one of the top 3 hits for the Google search for "how to suppress -Wunused-result in c++", so I'm adding this answer here since I figured it out and want to help the next person.
In case your warning/error is -Wunused (or one of its sub-errors) or -Wunused -Werror only, the solution is to cast to void:
For -Wunused or one of its sub-errors only1, you can just cast it to void to disable the warning. This should work for any compiler and any IDE for both C and C++.
1Note 1: see gcc documentation here, for example, for a list of these warnings: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, then search for the phrase "All the above -Wunused options combined" and look there for the main -Wunused warning and above it for its sub-warnings. The sub-warnings that -Wunused contains include:
-Wunused-but-set-parameter
-Wunused-but-set-variable
-Wunused-function
-Wunused-label
-Wunused-local-typedefs
-Wunused-parameter
-Wno-unused-result
-Wunused-variable
-Wunused-const-variable
-Wunused-const-variable=n
-Wunused-value
-Wunused = contains all of the above -Wunused options combined
Example of casting to void to suppress this warning:
// some "unused" variable you want to keep around
int some_var = 7;
// turn off `-Wunused` compiler warning for this one variable
// by casting it to void
(void)some_var; // <===== SOLUTION! ======
For C++, this also works on functions which return a variable marked with [[nodiscard]]:
C++ attribute: nodiscard (since C++17)
If a function declared nodiscard or a function returning an enumeration or class declared nodiscard by value is called from a discarded-value expression other than a cast to void, the compiler is encouraged to issue a warning.
(Source: https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
So, the solution is to cast the function call to void, as this is actually casting the value returned by the function (which is marked with the [[nodiscard]] attribute) to void.
Example:
// Some class or struct marked with the C++ `[[nodiscard]]` attribute
class [[nodiscard]] MyNodiscardClass
{
public:
// fill in class details here
private:
// fill in class details here
};
// Some function which returns a variable previously marked with
// with the C++ `[[nodiscard]]` attribute
MyNodiscardClass MyFunc()
{
MyNodiscardClass myNodiscardClass;
return myNodiscardClass;
}
int main(int argc, char *argv[])
{
// THE COMPILER WILL COMPLAIN ABOUT THIS FUNCTION CALL
// IF YOU HAVE `-Wunused` turned on, since you are
// discarding a "nodiscard" return type by calling this
// function and not using its returned value!
MyFunc();
// This is ok, however, as casing the returned value to
// `void` suppresses this `-Wunused` warning!
(void)MyFunc(); // <===== SOLUTION! ======
}
Lastly, you can also use the C++17 [[maybe_unused]] attribute: https://en.cppreference.com/w/cpp/language/attributes/maybe_unused.
If you want to disable unreferenced local variable write in some header
template<class T>
void ignore (const T & ) {}
and use
catch(const Except & excpt) {
ignore(excpt); // No warning
// ...
}
In certain situations you must have a named parameter but you don't use it directly.
For example, I ran into it on VS2010, when 'e' is used only inside a decltype statement, the compiler complains but you must have the named varible e.
All the above non-#pragma suggestions all boil down to just adding a single statement:
bool f(int e)
{
// code not using e
return true;
e; // use without doing anything
}
I have a class that is exported and it uses a header only structure as a member variable.
#include "SomeStruct.h"
class API_CLASS SomeClass
{
public:
// ...
private:
#pragma warning( push )
#pragma warning( disable: 4251 )
SomeStruct _active;
#pragma warning( pop )
};
I get warning 4251 as the structure is not exported. The structure is part of an external code and not a part of current scope.
I would like to make this code a bit more readable as this is a header I will distribute.
Ideally, I want the class to read
#include "SomeStruct.h"
class API_CLASS SomeClass
{
public:
// ...
private:
DISABLE_WARNING_BEGIN(4251)
SomeStruct _active;
DISABLE_WARNING_END
};
It may be more work to disable multiple warnings but if that can be done, then great but not necessary. The macro should result in code only for WIN32 environment. I have tried to write the macro but to include a '#' is beyond me.
I can live with this. This is better than what I have now.
#include "SomeStruct.h"
class API_CLASS SomeClass
{
public:
// ...
private:
#pragma warning(suppress: 4251)
SomeStruct _active;
};
Thanks to Heinirichj
To add #pragma into a MACRO, you may use:
__pragma with MSVC
_Pragma with gcc
See Pragma in define macro for more detail.
Is there a way to disable just a single warning line in a cpp file with visual studio?
For example, if I catch an exception and don't handle it, I get error 4101 (unreferenced local variable). Is there a way to ignore this just in that function, but otherwise report it in the compilation unit? At the moment, I put #pragma warning (disable : 4101) at the top of the file, but that obviously just turns it off for the whole unit.
#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
{
// ...
}
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
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 )
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
One may also use UNREFERENCED_PARAMETER defined in WinNT.H. The definition is just:
#define UNREFERENCED_PARAMETER(P) (P)
And use it like:
void OnMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
}
Why would you use it, you might argue that you can just omit the variable name itself. Well, there are cases (different project configuration, Debug/Release builds) where the variable might actually be used. In another configuration that variable stands unused (and hence the warning).
Some static code analysis may still give warning for this non-nonsensical statement (wParam;). In that case, you mayuse DBG_UNREFERENCED_PARAMETER which is same as UNREFERENCED_PARAMETER in debug builds, and does P=P in release build.
#define DBG_UNREFERENCED_PARAMETER(P) (P) = (P)
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).
This question comes up as one of the top 3 hits for the Google search for "how to suppress -Wunused-result in c++", so I'm adding this answer here since I figured it out and want to help the next person.
In case your warning/error is -Wunused (or one of its sub-errors) or -Wunused -Werror only, the solution is to cast to void:
For -Wunused or one of its sub-errors only1, you can just cast it to void to disable the warning. This should work for any compiler and any IDE for both C and C++.
1Note 1: see gcc documentation here, for example, for a list of these warnings: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, then search for the phrase "All the above -Wunused options combined" and look there for the main -Wunused warning and above it for its sub-warnings. The sub-warnings that -Wunused contains include:
-Wunused-but-set-parameter
-Wunused-but-set-variable
-Wunused-function
-Wunused-label
-Wunused-local-typedefs
-Wunused-parameter
-Wno-unused-result
-Wunused-variable
-Wunused-const-variable
-Wunused-const-variable=n
-Wunused-value
-Wunused = contains all of the above -Wunused options combined
Example of casting to void to suppress this warning:
// some "unused" variable you want to keep around
int some_var = 7;
// turn off `-Wunused` compiler warning for this one variable
// by casting it to void
(void)some_var; // <===== SOLUTION! ======
For C++, this also works on functions which return a variable marked with [[nodiscard]]:
C++ attribute: nodiscard (since C++17)
If a function declared nodiscard or a function returning an enumeration or class declared nodiscard by value is called from a discarded-value expression other than a cast to void, the compiler is encouraged to issue a warning.
(Source: https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
So, the solution is to cast the function call to void, as this is actually casting the value returned by the function (which is marked with the [[nodiscard]] attribute) to void.
Example:
// Some class or struct marked with the C++ `[[nodiscard]]` attribute
class [[nodiscard]] MyNodiscardClass
{
public:
// fill in class details here
private:
// fill in class details here
};
// Some function which returns a variable previously marked with
// with the C++ `[[nodiscard]]` attribute
MyNodiscardClass MyFunc()
{
MyNodiscardClass myNodiscardClass;
return myNodiscardClass;
}
int main(int argc, char *argv[])
{
// THE COMPILER WILL COMPLAIN ABOUT THIS FUNCTION CALL
// IF YOU HAVE `-Wunused` turned on, since you are
// discarding a "nodiscard" return type by calling this
// function and not using its returned value!
MyFunc();
// This is ok, however, as casing the returned value to
// `void` suppresses this `-Wunused` warning!
(void)MyFunc(); // <===== SOLUTION! ======
}
Lastly, you can also use the C++17 [[maybe_unused]] attribute: https://en.cppreference.com/w/cpp/language/attributes/maybe_unused.
If you want to disable unreferenced local variable write in some header
template<class T>
void ignore (const T & ) {}
and use
catch(const Except & excpt) {
ignore(excpt); // No warning
// ...
}
In certain situations you must have a named parameter but you don't use it directly.
For example, I ran into it on VS2010, when 'e' is used only inside a decltype statement, the compiler complains but you must have the named varible e.
All the above non-#pragma suggestions all boil down to just adding a single statement:
bool f(int e)
{
// code not using e
return true;
e; // use without doing anything
}
In my code, I am using an array xyz of 10 objects. When I am trying to access an element of the array using an unsigned int index like this: xyz[level], I get 'Buffer overrun' warning. Logically, I am pretty sure that level won't exceed 10. How to avoid this warning?
I'm probably teaching my grandmother to suck eggs here, but do remember that "level won't exceed 10" is wrong for an array of size 10:
char a[10];
a[10] = '\0'; // Bug, and "Buffer Overrun" warning.
Are you really sure? I never got this warning until now. So, double check.
Anyway, you can use the
#pragma warning( disable: 6386 )
preprocessor directive. I usually push and pop this to the "pragma stack"
#pragma warning( push )
#pragma warning( disable : 6386 )
// Some code
#pragma warning( pop )
as advised here.