Answers:
#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop )
clang似乎不支持这种编译,但你可以实现与同样的效果#pragma clang diagnostic push,#pragma clang diagnostic ignored "-Wunused-variable"和#pragma clang diagnostic pop。请参见“控制Diagnositics公司通过编译指示”,在锵用户手册
/wd4101。请注意:,标志和数字之间不是正常的,并且您不能用逗号分隔数字列表。对于其他编译器,可能会/nowarn:4101改为。
如果只想在一行代码中禁止显示警告,则可以使用suppress 警告说明符:
#pragma warning(suppress: 4101)
// here goes your single line of code where the warning occurs
对于单行代码,其工作原理与编写以下代码相同:
#pragma warning(push)
#pragma warning(disable: 4101)
// here goes your code where the warning occurs
#pragma warning(pop)
suppress说明符在经过预处理的一行代码上运行。如果下面的行#pragma warning(suppress: ...)是#include指令(它将其参数引用的文件扩展到当前编译单元中),则该效果仅适用于该文件的第一行。这应该是显而易见的,因为警告是由编译器生成的。编译器对预处理的代码进行操作。
#pragma push / pop通常是解决此类问题的方法,但是在这种情况下,为什么不删除未引用的变量呢?
try
{
// ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
// ...
}
:P)。
例:
#pragma warning(suppress:0000) // (suppress one error in the next line)
此编译指示对于从Visual Studio 2005开始的C ++有效。
。https: v= .aspx
该杂项对于通过Visual Studio 2005到Visual Studio 2015的C#无效。
错误:“预期禁用或还原”。
(我想他们永远都无法实现suppress...)https://msdn.microsoft.com/zh-cn/library/441722ys(v=vs.140 )
.aspx
C#需要不同的格式。它看起来像这样(但不起作用):
#pragma warning suppress 0642 // (suppress one error in the next line)
相反的suppress,你要disable和enable:
if (condition)
#pragma warning disable 0642
; // Empty statement HERE provokes Warning: "Possible mistaken empty statement" (CS0642)
#pragma warning restore 0642
else
太丑了,我认为重新设置样式会更聪明:
if (condition)
{
// Do nothing (because blah blah blah).
}
else
也可以使用中UNREFERENCED_PARAMETER定义的WinNT.H。定义是:
#define UNREFERENCED_PARAMETER(P) (P)
并像这样使用它:
void OnMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
}
为什么要使用它,您可能会争辩说您可以忽略变量名本身。好吧,在某些情况下(不同的项目配置,调试/发布版本),实际可能会使用该变量。在另一种配置中,该变量处于未使用状态(因此出现警告)。
某些静态代码分析可能仍会对此不切实际的声明(wParam;)发出警告。在这种情况下,您DBG_UNREFERENCED_PARAMETER可以使用与UNREFERENCED_PARAMETER调试版本相同的功能,而P=P在发行版本中使用相同的功能。
#define DBG_UNREFERENCED_PARAMETER(P) (P) = (P)
[[maybe_unused]]属性
如果要禁用unreferenced local variable某些标头中的写入
template<class T>
void ignore (const T & ) {}
和使用
catch(const Except & excpt) {
ignore(excpt); // No warning
// ...
}
(void)unusedVar;呢?
(void)unusedVar;?不符合C ++标准。
static_cast<void>(unusedVar)。
Any expression can be explicitly converted to type “cv void.” The expression value is discarded根据您可以编写static_cast<void>(unusedVar)和static_cast<const void>(unusedVar)和static_cast<volatile void>(unusedVar)。所有表格均有效。我希望它可以澄清您的疑问。
在某些情况下,您必须具有命名参数,但不能直接使用它。
例如,我在VS2010上遇到过这种情况,当仅在decltype语句中使用'e'时,编译器会抱怨,但您必须具有命名的varible e。
以上所有非#pragma建议都归结为仅添加一条语句:
bool f(int e)
{
// code not using e
return true;
e; // use without doing anything
}
catch (const std::exception& /* unnamed */) {.... }。它不能回答您的问题,但可以解决您的问题。