# Define STR _ (x) x # define debug (format ,...) fprintf (stderr, "% s: % d:" str _ (Format) "\ n", _ file __, _ line __, ##__ va_args __) # define assert _ (expr _, extra_op) do {If (! (Expr _) {debug ("in the function '% s': asserted error:" # expr _, _ function _); \ extra_op; exit (-1) ;}}while (0) # define assert (expr) assert _ (expr ,)
I don't like if (! Func () {print ("Fail !! "); Exit. Assert is more concise.
Compared with the original assert, the self-made assert has an extra_op (similar to finally or rescue) and can perform other operations when an asserted error occurs, not necessarily exit. Of course, you do not need to consider the debug, release, void return values, and macro expansion issues.
The problem is that you cannot write int A = assert (func) or assert (int A = func) to wrap the variable declaration and assert in the same row.