And*_*nck 165
#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop )
Run Code Online (Sandbox Code Playgroud)
Dan*_*her 80
如果您只想在一行代码中禁止警告,则可以使用suppress 警告说明符:
#pragma warning(suppress: 4101)
// here goes your single line of code where the warning occurs
Run Code Online (Sandbox Code Playgroud)
对于单行代码,这与编写以下代码相同:
#pragma warning(push)
#pragma warning(disable: 4101)
// here goes your code where the warning occurs
#pragma warning(pop)
Run Code Online (Sandbox Code Playgroud)
Mat*_*lia 28
#pragma push/pop通常是解决这类问题的方法,但在这种情况下你为什么不删除未引用的变量呢?
try
{
// ...
}
catch(const your_exception_type &) // type specified but no variable declared
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
#pragma warning ( push )然后使用,然后输入#pragma warning ( disable )您的代码,然后#pragma warning ( pop )按照此处的描述使用:
#pragma warning( push )
#pragma warning( disable : WarningCode)
// code with warning
#pragma warning( pop )
Run Code Online (Sandbox Code Playgroud)
正如@rampion 提到的,如果您在 clang gcc 中,警告是按名称而不是编号,您需要执行以下操作:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
// ..your code..
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)
这个信息来自这里
这个问题是Google 搜索“如何在 c++ 中抑制 -Wunused-result” 的前 3 个点击之一,所以我在这里添加这个答案,因为我找到了它并希望帮助下一个人。
-Wunused(或其子错误之一)或-Wunused -Werror 仅,解决方案是转换为void:对于仅1-Wunused或其子错误之一,您可以将其强制转换为以禁用警告。这应该适用于任何 C 和 C++ 编译器以及任何 IDE。void
1注 1:例如,请参阅此处的 gcc 文档,了解这些警告的列表:https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html,然后搜索短语“以上所有 -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选项的组合强制转换 to 来抑制此警告的示例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! ======
Run Code Online (Sandbox Code Playgroud)
对于 C++,这也适用于返回标有 的变量的函数[[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! ======
}
Run Code Online (Sandbox Code Playgroud)
最后,您还可以使用 C++17[[maybe_unused]]属性:https://en.cppreference.com/w/cpp/language/attributes/maybe_unused。
也可以使用中UNREFERENCED_PARAMETER定义的WinNT.H。定义是:
#define UNREFERENCED_PARAMETER(P) (P)
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
void OnMessage(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
}
Run Code Online (Sandbox Code Playgroud)
为什么要使用它,您可能会争辩说您可以忽略变量名本身。好吧,在某些情况下(不同的项目配置,调试/发布版本),实际可能会使用该变量。在另一种配置中,该变量处于未使用状态(因此出现警告)。
某些静态代码分析可能仍会对此不切实际的声明(wParam;)发出警告。在这种情况下,您DBG_UNREFERENCED_PARAMETER可以使用与UNREFERENCED_PARAMETER调试版本相同的功能,而P=P在发行版本中使用相同的功能。
#define DBG_UNREFERENCED_PARAMETER(P) (P) = (P)
Run Code Online (Sandbox Code Playgroud)
例:
#pragma warning(suppress:0000) // (suppress one error in the next line)
Run Code Online (Sandbox Code Playgroud)
此编译指示对于从Visual Studio 2005开始的C ++有效。https: //msdn.microsoft.com/zh-cn/library/2c8f766e( v= vs.80)
.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)
Run Code Online (Sandbox Code Playgroud)
而不是suppress,您必须disable和enable:
if (condition)
#pragma warning disable 0642
; // Empty statement HERE provokes Warning: "Possible mistaken empty statement" (CS0642)
#pragma warning restore 0642
else
Run Code Online (Sandbox Code Playgroud)
这太丑了,我认为重新设置样式会更聪明:
if (condition)
{
// Do nothing (because blah blah blah).
}
else
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117823 次 |
| 最近记录: |