Is there a way to disable all warnings with a pragma?

我的未来我决定 提交于 2019-11-28 22:28:30

You can push/pop a low level of warning, like this:

#pragma warning(push, 0)        

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
// ...

#pragma warning(pop)

But know that it's not possible to disable all warnings. For example, some linker warnings are impossible to turn off.

#pragma warning(disable:4820)
#pragma warning(disable:4619)
#pragma warning(disable:4668)

for less lines....

#pragma warning (disable : 4820 4619 4668)

What I've done before is set the "W3" option rather than "Wall" then in each of my own source .cpp files I put

#pragma warning(push, 4)

at the top AFTER all the "#include..." lines and then

#pragma warning(pop)

as the very last line of the file.

This way you get level 4 warnings in your code and level 3 in 3rd party code that you can't do anything about.

Using the technique described in the Assaf Lavie's answer it is possible to create helper macros:

#define DISABLE_ALL_WARNINGS_BEGIN \
    __pragma(warning(push, 0))

#define DISABLE_ALL_WARNINGS_END \
    __pragma(warning(pop))

They can be used in the following way (online demo):

DISABLE_ALL_WARNINGS_BEGIN
void foo(int a)
{
    // this function should generate the following warning
    // "warning C4100: 'a': unreferenced formal parameter"
    // unless it is not wrapped inside "DISABLE_ALL_WARNINGS" section
}
DISABLE_ALL_WARNINGS_END
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!