Avoiding unused variables warnings when using assert() in a Release build

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

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.


I wouldn't be able to give a better answer than this, that addresses that problem, and many more:

Stupid C++ Tricks: Adventures in assert

#ifdef NDEBUG
#define ASSERT(x) do { (void)sizeof(x);} while (0)
#else
#include <assert.h>
#define ASSERT(x) assert(x)
#endif

As of C++17, the variable can be decorated with an attribute.

[[maybe_unused]] int Result = Func();
assert( Result == 1 );

See https://en.cppreference.com/w/cpp/language/attributes/maybe_unused for details.

This is better than the (void)Result trick because you directly decorate the variable declaration, rather than add something as an afterthought.