且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在发布版本中使用 assert() 时避免未使用的变量警告

更新时间:2023-11-25 07:50:03

我们使用一个宏来专门指示什么时候没有使用:

We use a macro to specifically indicate when something is unused:

#define _unused(x) ((void)(x))

那么在您的示例中,您将:

Then in your example, you'd have:

int Result = Func();
assert( Result == 1 );
_unused( Result ); // make production build happy

这样 (a) 生产构建成功,并且 (b) 在代码中很明显,该变量按设计未被使用,而不是只是被遗忘了.这在不使用函数参数时特别有用.

That way (a) the production build succeeds, and (b) it is obvious in the code that the variable is unused by design, not that it's just been forgotten about. This is especially helpful when parameters to a function are not used.