C++ 避免警告“未引用的形式参数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3020584/
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
Avoid warning 'Unreferenced Formal Parameter'
提问by bdhar
I have a super class like this:
我有一个这样的超级班:
class Parent
{
public:
virtual void Function(int param);
};
void Parent::Function(int param)
{
std::cout << param << std::endl;
}
..and a sub-class like this:
..和这样的子类:
class Child : public Parent
{
public:
void Function(int param);
};
void Child::Function(int param)
{
;//Do nothing
}
When I compile the sub-class .cpp file, I get this error
当我编译子类 .cpp 文件时,出现此错误
warning C4100: 'param' : unreferenced formal parameter
As a practice, we used to treat warnings as errors. How to avoid the above warning?
作为一种实践,我们曾经将警告视为错误。如何避免上述警告?
Thanks.
谢谢。
回答by CB Bailey
In C++ you don't have to give a parameter that you aren't using a name so you can just do this:
在 C++ 中,您不必提供未使用名称的参数,因此您可以这样做:
void Child::Function(int)
{
//Do nothing
}
You may wish to keep the parameter name in the declaration in the header file by way of documentation, though. The empty statement (;
) is also unnecessary.
不过,您可能希望通过文档的方式将参数名称保留在头文件的声明中。空语句 ( ;
) 也是不必要的。
回答by Charles Oppermann
I prefer using a macro, as it tells not only the compiler my intention, but other maintainers of the code, and it's searchable later on.
我更喜欢使用宏,因为它不仅告诉编译器我的意图,而且告诉代码的其他维护者,并且以后可以搜索它。
The method of commenting out the argument name can easily be missed by people unfamiliar with the code (or me 6 months later).
不熟悉代码的人(或 6 个月后的我)很容易错过注释掉参数名称的方法。
However, it's a style-issue, neither method is "better" or more optimal with regards to code generated, performance or robustness. To me, the deciding factor is informing others of my intent through a standardized system. Omitting the parameter name and putting in a comment would work equally well:
然而,这是一个风格问题,就生成的代码、性能或健壮性而言,这两种方法都不是“更好”或更优化的方法。对我而言,决定性因素是通过标准化系统将我的意图告知他人。省略参数名称并添加注释同样有效:
void CFooBar::OnLvnItemchanged(NMHDR *pNMHDR, LRESULT *pResult)
{
UNREFERENCED_PARAMETER(pNMHDR);
Alternatively:
或者:
void CFooBar::OnLvnItemchanged(NMHDR* /* pNMHDR */, LRESULT *pResult)
{
// Not using: pNMHDR
I would say that the worst solution is suppressing the warning message; that that will affect your entire file or project, and you'll lose the knowledge that maybe you've missed something. At least by adding the macro, or commenting out the argument name, you've told others that you've made a conscious decision to not use this argument and that it's not a mistake.
我会说最糟糕的解决方案是抑制警告消息;这将影响您的整个文件或项目,并且您将失去可能错过某些内容的知识。至少通过添加宏或注释掉参数名称,您已经告诉其他人您已经做出了不使用此参数的有意识的决定,这不是错误。
The Windows SDK in WinNT.h defines UNREFERENCED_PARAMETER()
along with DBG_UNREFERENCED_PARAMETER()
and DBG_UNREFERENCED_LOCAL_VARIABLE()
. They all evaluate to the same thing, but the difference is that DBG_UNREFERENCED_PARAMETER() is used when you are starting out and expect to use the parameter when the code is more complete. When you are sure you'll never use the parameter, use the UNREFERENCED_PARAMETER() version.
WinNT.h 中的 Windows SDKUNREFERENCED_PARAMETER()
与DBG_UNREFERENCED_PARAMETER()
和一起定义DBG_UNREFERENCED_LOCAL_VARIABLE()
。它们都评估为相同的东西,但不同的是 DBG_UNREFERENCED_PARAMETER() 在您开始时使用,并希望在代码更完整时使用该参数。当您确定永远不会使用该参数时,请使用 UNREFERENCED_PARAMETER() 版本。
The Microsoft Foundation Classes (MFC) have a similar convention, with the shorter UNUSED()
and UNUSED_ALWAYS()
macros.
Microsoft 基础类 (MFC) 具有类似的约定,具有更短的UNUSED()
和UNUSED_ALWAYS()
宏。
Pick a style and stick with it. That way later on you can search for "DBG_UNREFERENCED_PARAMETER
" in your code and find any instances of where you expected to use a argument, but didn't. By adopting a consistent style, and habitually using it, you'll make it easier for other and yourself later on.
选择一种风格并坚持下去。这样,稍后您可以DBG_UNREFERENCED_PARAMETER
在代码中搜索“ ”,并找到您希望使用参数但没有使用的任何实例。通过采用一致的风格,并习惯性地使用它,你以后会更容易为他人和自己服务。
回答by R Samuel Klatchko
Another technique that you can use if you want to keep the parameter name is to cast to void:
如果您想保留参数名称,您可以使用的另一种技术是强制转换为 void:
void Child::Function(int param)
{
(void)param; //Do nothing
}
回答by Franci Penov
As @Charles Bailey mentioned, you can skip the parameter name.
正如@Charles Bailey 提到的,您可以跳过参数名称。
However, in certain scenarios, you need the parameter name, since in debug builds you are calling an ASSERT()
on it, but on retail builds it's a nop
. For those scenarios there's a handy macros (at least in VC++ :-)) UNREFERENCED_PARAMETER()
, which is defined like this:
但是,在某些情况下,您需要参数名称,因为在调试版本中您正在调用ASSERT()
它,但在零售版本中它是nop
. 对于这些场景,有一个方便的宏(至少在 VC++ :-)) 中UNREFERENCED_PARAMETER()
,其定义如下:
#define UNREFERENCED_PARAMETER(x) x
Note that the simple cast @R Samuel Klatchko posted also works, but I personally find it more readable if the code is explicit that this is an unreferenced parameter vs. simple unexplained cast like that.
请注意,@R Samuel Klatchko 发布的简单强制转换也有效,但我个人认为,如果代码明确指出这是一个未引用的参数,而不是像这样的简单无法解释的强制转换,则它更具可读性。
回答by Dwayne Robinson
Pragma works nicely too since it's clear you are using VS. This warning has a very high noise to benefit ratio, given that unreferenced parameters are very common in callback interfaces and derived methods. Even teams within Microsoft Windows who use W4 have become tired of its pointlessness (would be more suitable for /Wall) and simply added to their project:
Pragma 也很好用,因为很明显您正在使用 VS。考虑到未引用的参数在回调接口和派生方法中非常常见,此警告的噪声与收益比非常高。甚至 Microsoft Windows 中使用 W4 的团队也厌倦了它的无意义(更适合 /Wall)并简单地添加到他们的项目中:
#pragma warning(disable: 4100)
If you want to alleviate the warning for just a block of code, surround it with:
如果您只想缓解一段代码的警告,请将其括起来:
#pragma warning(push)
#pragma warning(disable: 4100)
void SomeCallbackOrOverride(int x, float y) { }
#pragma warning(pop)
The practice of leaving out the parameter name has the downside in the debugger that you can't easily inspect by name nor add it to the watch (becomes confused if you have more than one unreferenced parameter), and while a particular implementation of a method may not use the parameter, knowing its value can help you figure out which stage of a process you are in, especially when you do not have the whole call stack above you.
省略参数名称的做法在调试器中有一个缺点,即您不能轻松地按名称检查或将其添加到监视中(如果您有多个未引用的参数,则会变得混乱),并且方法的特定实现可能不使用该参数,知道它的值可以帮助您确定您处于进程的哪个阶段,尤其是当您没有整个调用堆栈时。
回答by TheBeardyMan
I would use a macro to suppress the unreferenced formal parameter warning:
我会使用宏来抑制未引用的形式参数警告:
#define UNUSED( x ) ( &reinterpret_cast< const int& >( x ) )
This has the following advantages:
这具有以下优点:
- Unlike #define UNUSED( x ) ( void )x, it doesn't introduce a need for the full definition of the parameter's type to be seen where no such need may have existed before.
- Unlike #define UNUSED( x ) &x, it can be used safely with parameters whose types overload the unary & operator.
- 与 #define UNUSED( x ) ( void )x 不同,它不需要在以前可能不存在这种需要的地方看到参数类型的完整定义。
- 与 #define UNUSED( x ) &x 不同,它可以安全地与类型重载一元 & 运算符的参数一起使用。
回答by Gwydion
What about just adding reference with a comment:
只添加带有评论的引用怎么样:
void Child::Function(int param)
{
param; //silence unreferenced warning
}
This was also suggested here: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4100?view=vs-2019
这里也建议这样做:https: //docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4100?view=vs-2019