Some time ago, I made this beautiful assert macro for c and c++ programs
#define ASSERT(truthy, message) \
if (!(truthy)) \
{\
cout << message << " on line " << __LINE__ << " in file " << __FILE__ << ". Check was " << #truthy << endl;\
}
Scatter ASSERT calls throughout your code, and it will warn you whenever the truthy value is not truthy! Very handy during development to remind you of potential mistakes.
ex
ASSERT(filesFound > 0, "Couldn't find any files, check your path!");
When filesFound is 0, the macro will print out
Couldn't find any files, check your path! on line 27 in file openFiles.c. Check was filesFound > 0
Now what I want it to print, to give me even more relevant information, is the value of any variables passed into the truthy parameter. Like this
Couldn't find any files, check your path! on line 27 in file openFiles.c. Check was filesFound > 0, filesFound is 0
This seems lisp-like territory, I wonder, is there any black magic c preprocessing that I can use to evaluate variables and functions to their values, without evaluating the truthy statement?
I assume to be disappointed.
do{ ... }while(0);statement, then you can use variables local to thedo-while. Note that the standardassertoften prints relevant information as well. A common hack is to doassert(truthy && "The foo was barred!");to print the message with the expression.do {…} while (false)is so that the user of the macro is forced/enabled to put a semicolon after the macro invocation. The local variables would also work in OP’s macro.do{ int val = ... }while(0);such thatvalis not visible outside the macro and doesn't interfere with other local variables.do…whilefor that here, the same works in OP’s code inside the scope of theifbody.