There are problems with this macro check status and return if failed

we have frequently repeated lines in the code that check the return status of the function, and if it returns immediately with the status. I thought about defining a macro for this:

#define RETURN_IF_FAILED(x) { int stat = (x); if (FAILED(stat)) return stat; }

A local variable is that a parameter xcan be a function call.

As I know, there are some strange artifacts of using macros, and I don't really understand them, I would like to ask if there are any problems with this macro. Thank.

(and please do not suggest using exceptions - I hate this style myself, but the way it is done here)

+3
source share
1 answer

; do{} while(0) :

#define RETURN_IF_FAILED(x) do { int stat = (x); if (FAILED(stat)) return stat; } while(0)

MACRO, :

if (SomeCondition)
  RETURN_IF_FAILED(x); //<--- note the "usual" semicolon!
else
{
   //some code
}

. ; !

+7

Source: https://habr.com/ru/post/1783855/


All Articles