cross platform macro for silencing unused variables warning

此生再无相见时 提交于 2019-11-29 06:54:38

Yup - you can use this approach for GCC and Clang:

#define MON_Internal_UnusedStringify(macro_arg_string_literal) #macro_arg_string_literal

#define MONUnusedParameter(macro_arg_parameter) _Pragma(MON_Internal_UnusedStringify(unused(macro_arg_parameter)))

although mine did have the (void) approach defined for clang, it appears that Clang now supports the stringify and _Pragma approach above. _Pragma is C99.

(void)x;

is fine; has always worked for me. You can't usually expand a macro to a #pragma, although there is usually a slightly different pragma syntax that can be generated from a macro (_Pragma on gcc and clang, __pragma on VisualC++).

Still, I don't actually need the (void)x anymore in C++, since you can simply not give a name to a function parameter to indicate that you don't use it:

int Example(int, int b, int)
{
   ... /* only uses b */
}

works perfectly fine.

#define and #pragma both are preprocessor directives. You cannot define one macro to expand as preprocessor directive. Following would be incorrect:

#define MY_MACRO   #if _WIN32 

MY_MACRO cannot expand to #if _WIN32 for the compiler.

Your best bet is to define your own macro:

#define UNUSED(_var) _var
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!