有没有一种方法可以在Visual Studio中禁用cpp文件中的单个警告行?
例如,如果我捕获了一个异常但没有处理它,那么我会得到错误4101(未引用的局部变量)。是否有一种方法可以仅在该函数中忽略此警告行,但在编译单元中报告其他警告行?目前,我将#pragma warning (disable : 4101)
放在文件顶部,但这显然只会关闭整个编译单元的警告。
有没有一种方法可以在Visual Studio中禁用cpp文件中的单个警告行?
例如,如果我捕获了一个异常但没有处理它,那么我会得到错误4101(未引用的局部变量)。是否有一种方法可以仅在该函数中忽略此警告行,但在编译单元中报告其他警告行?目前,我将#pragma warning (disable : 4101)
放在文件顶部,但这显然只会关闭整个编译单元的警告。
#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop )
#pragma clang diagnostic push
、#pragma clang diagnostic ignored "-Wunused-variable"
和#pragma clang diagnostic pop
来实现相同的效果。请参见Clang用户手册中的“通过Pragmas控制诊断”一节。 - rampion/wd4101
。请注意,标志和数字之间没有正常的:
,并且您不能使用逗号分隔的数字列表。对于其他编译器,可能会改为/nowarn:4101
。 - Jesse Chisholm如果您只想在单行代码(预处理后)中抑制警告[1],则可以使用suppress
warning specifier:
#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)
[1] 在下面的评论中,其他人已经注意到如果以下语句是一个#include语句,那么#pragma warning(suppress: 4101)语句将不能有效地抑制头文件中每一行的警告。如果有意这样做,就需要使用push/disable/pop方法。
suppress
修饰符作用于一行预处理代码。如果#pragma warning(suppress: ...)
之后的行是一个#include
指令(将其参数所引用的文件扩展到当前编译单元),则该效果仅适用于该文件的第一行。这显而易见,因为编译器生成警告。编译器对预处理代码进行操作。 - IInspectable#pragma warning(suppress: 4101)
\n #include "wholeFile.h
不会抑制整个头文件中的错误(显然,但注释通常指向不明显的行为,因此会引起困惑)。你可以在头文件本身中使用它,没问题。 - Anne Quinn#pragma
push/pop通常用于解决这种问题,但在这种情况下为什么不直接删除未被引用的变量呢?
try
{
// ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
// ...
}
/kernel
开关进行编译。 - Chef Gladiator例子:
#pragma warning(suppress:0000) // (suppress one error in the next line)
这个编译指示在 Visual Studio 2005 开始适用于C++。
https://msdn.microsoft.com/en-us/library/2c8f766e(v=vs.80).aspx
这个编译指示在 Visual Studio 2005 到 Visual Studio 2015 中不适用于C#,会出现错误:“Expected disable or restore”。
(我猜他们从来没有实现过suppress
...)
https://msdn.microsoft.com/en-us/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
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// ..your code..
#pragma clang diagnostic pop
这些信息来自这里
使用 #pragma warning ( push )
,然后使用 #pragma warning ( disable )
,再编写你的代码,最后使用 #pragma warning ( pop )
。详细说明可参见这里:
#pragma warning( push )
#pragma warning( disable : WarningCode)
// code with warning
#pragma warning( pop )
expression must have a constant value C/C++(28)
。但是把警告代码写作28并没有起作用! - Sourav Kannantha B也可以使用在 WinNT.H
中定义的 UNREFERENCED_PARAMETER
。其定义如下:
#define UNREFERENCED_PARAMETER(P) (P)
并且像这样使用:
void OnMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
}
你可能会争辩为什么要使用它,因为你可以省略变量名本身。然而,在某些情况下(不同项目配置,Debug/Release构建),该变量可能实际被使用。在另一个配置中,该变量未使用(因此会产生警告)。
一些静态代码分析仍可能对这个无意义的语句(wParam;
)发出警告。在这种情况下,你可以使用 DBG_UNREFERENCED_PARAMETER
,在调试构建中与 UNREFERENCED_PARAMETER
相同,在发布构建中执行 P=P
。
#define DBG_UNREFERENCED_PARAMETER(P) (P) = (P)
[[maybe_unused]]
属性。 - metablaster-Wunused
(或其子错误之一)或-Wunused -Werror
,那么解决方案是强制转换为void
:对于仅有1-Wunused
或其子错误之一,您可以将其强制转换为void
以禁用警告。这适用于任何编译器和IDE,适用于C和C++。
1注意1:请参阅gcc文档这里,例如,获取这些警告的列表,然后在其中搜索短语“所有上述-Wunused选项组合”并查找主要-Wunused
警告及其子警告。 -Wunused
包含的子警告包括:
-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
=包含所有-Wunused
选项抑制此警告的方法之一是将数据转换为 void
类型,例如:
// 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! ======
[[nodiscard]]
的变量的函数:
因此,解决方案是将函数调用转换为C++属性:nodiscard(自C++17起)
如果在从除了转换为void的废弃值表达式中调用声明为nodiscard的函数或返回枚举或类的函数被值声明为nodiscard,则编译器会建议发出警告。
(来源:https://en.cppreference.com/w/cpp/language/attributes/nodiscard)
void
,因为这实际上是将由带有[[nodiscard]]
属性的函数返回的值强制转换为void
。// 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! ======
}
最后,你也可以使用C++17的[[maybe_unused]]
属性:https://en.cppreference.com/w/cpp/language/attributes/maybe_unused。
#pragma warning (push)
、#pragma warning (disable)
和相应的#pragma warning (pop)
将相关代码包裹起来,如此处所示。虽然还有其他选项,包括#pramga warning (once)
。template<class T>
void ignore (const T & ) {}
并且使用
catch(const Except & excpt) {
ignore(excpt); // No warning
// ...
}
(void)unusedVar;
? - Nawaz(void)unusedVar;
不符合C++标准。 - Alexey Malistovstatic_cast<void>(unusedVar)
。 - Nawazstatic_cast<void>(unusedVar)
,static_cast<const void>(unusedVar)
和 static_cast<volatile void>(unusedVar)
。所有形式都是有效的。希望这能解决你的疑惑。 - Nawaz
catch (const std::exception& /* unnamed */) {.... }
。这并没有回答你的问题,但可能解决了你的问题。 - Sjoerd