void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
获取编译错误,甚至在代码中也不想发出警告。
自C ++ 17起,我们有了maybe_unused
attribute来抑制未使用实体的警告:
maybe_unused
很简单。如果void func([[maybe_unused]] char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
为NOT定义,则函数中未使用变量_XYZ_
,并且编译器发出减弱的提示,可以将其转换为:
翻译自编译器:
我的朋友,你问我一块名叫
param
的记忆,但你是不使用它。确定要这样做吗?
在C ++ 17的param
属性之前,可以使用特定于编译器的命令。
在MSVC中,您可以这样做:
[[maybe_unused]]
在Borland / Embarcadero,您可以这样做:
#pragma warning(push)
#pragma warning(disable:4101)
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
#pragma warning(pop)
在GCC中,您可以使用以下之一:
#pragma argsused
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
void func(__attribute__((unused)) char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
void func(__unused char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
或者,您可以简单地引用该参数,而无需使用任何特定于编译器的技巧:
void func([[gnu::unused]] char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
非常了解C ++ 17的方式,但是,如果可能的话,我们可以像这样更改函数:
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#else
(void)param;
#endif
}