C++ 禁用单个警告错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7159348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:32:56  来源:igfitidea点击:

Disable single warning error

c++visual-c++warningspragma

提问by Cookie

Is there a way to disable just a single warning line in a cpp file with visual studio?

有没有办法使用visual studio禁用cpp文件中的单个警告行?

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.

例如,如果我捕获一个异常并且不处理它,我会收到错误 4101(未引用的局部变量)。有没有办法在该函数中忽略它,但在编译单元中报告它?目前,我把它放在#pragma warning (disable : 4101)文件的顶部,但这显然只是关闭了整个单元。

回答by Andreas Brinck

#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop ) 

回答by Daniel Seither

If you only want to suppress a warning in a single line of code, you can use the suppresswarning specifier:

如果只想在一行代码中抑制警告,可以使用suppress警告说明符

#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)

回答by Matteo Italia

#pragmapush/pop are often a solution for this kind of problems, but in this case why don't you just remove the unreferenced variable?

#pragmapush/pop 通常是此类问题的解决方案,但在这种情况下,为什么不删除未引用的变量呢?

try
{
    // ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
    // ...
}

回答by sharptooth

Use #pragma warning ( push ), then #pragma warning ( disable ), then put your code, then use #pragma warning ( pop )as described here:

使用#pragma warning ( push ), then #pragma warning ( disable ),然后放置您的代码,然后#pragma warning ( pop )按照此处的说明使用:

#pragma warning( push )
#pragma warning( disable : WarningCode)
// code with warning
#pragma warning( pop ) 

回答by A876

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

此编译指示对从 Visual Studio 2005 开始的C++有效。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#到 Visual Studio 2005 到 Visual Studio 2015
无效。错误:“预期禁用或恢复”。
(我猜他们从来没有开始实施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):

C# 需要不同的格式。它看起来像这样(但不起作用):

#pragma warning suppress 0642  // (suppress one error in the next line)

Instead of suppress, you have to disableand enable:

而不是suppress,你必须disableenable

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

回答by Christian.K

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.

相反,把它放在文件(甚至是头文件)之上的,只是包装与问题的代码#pragma warning (push)#pragma warning (disable)以及匹配#pragma warning (pop),如图所示这里

Although there are some other options, including #pramga warning (once).

尽管还有其他一些选项,包括#pramga warning (once).

回答by Ajay

One may also use UNREFERENCED_PARAMETERdefined in WinNT.H. The definition is just:

也可以使用中UNREFERENCED_PARAMETER定义的WinNT.H。定义只是:

#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_PARAMETERwhich is same as UNREFERENCED_PARAMETERin debug builds, and does P=Pin release build.

某些静态代码分析可能仍会针对此无意义的语句 ( wParam;)发出警告。在这种情况下,您DBG_UNREFERENCED_PARAMETER可以使用与UNREFERENCED_PARAMETER调试版本相同的方法,并P=P在发布版本中使用。

#define DBG_UNREFERENCED_PARAMETER(P)      (P) = (P)

回答by Adi Shavit

In certain situations you musthave 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 decltypestatement, the compiler complains but you must have the named varible e.

在某些情况下,您必须有一个命名参数,但不直接使用它。
例如,我在 VS2010 上遇到了它,当 'e' 仅在decltype语句中使用时,编译器会抱怨,但您必须具有命名变量e

All the above non-#pragmasuggestions all boil down to just adding a single statement:

以上所有非#pragma建议都归结为只添加一个语句:

bool f(int e)
{ 
   // code not using e
   return true;
   e; // use without doing anything
}

回答by Alexey Malistov

If you want to disable unreferenced local variablewrite in some header

如果您想unreferenced local variable在某些标题中禁用写入

template<class T>
void ignore (const T & ) {}

and use

并使用

catch(const Except & excpt) {
    ignore(excpt); // No warning
    // ...  
} 

回答by orion elenzil

as @rampion mentioned, if you are in clang gcc, the warnings are by name, not number, and you'll need to do:

正如@rampion 所提到的,如果您在 clang gcc 中,警告是按名称而不是编号,您需要执行以下操作:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// ..your code..
#pragma clang diagnostic pop

this info comes from here

这个信息来自这里