Re: [gtk-list] g_assert() and consorts into {}?



Tim> i'd like to see all the macros around g_assert ()
Tim> (return_if_fail...), to be enclosed by {}.

Tim> #define g_assert(expr) \
Tim> {						     \
Tim  [ ... ]
Tim> }

Tim> this will avoid prblems with code snippets like

Tim> if (use_ballon)
Tim>   g_assert (ballon != NULL);
Tim> else
Tim>   g_assert (ball != NULL);

Tim> any objections?


This won't work.  The expanded code in the case you show will look
like:

	if (foo) { ... }; else { ... };
                        ^ oops

... which isn't valid C.


The standard trick for doing what you really want is to wrap the macro
in a do...while:

#define g_assert(expr) \
   do { \
 [ ... ]
  } while (0)

That way a semicolon can be appended with no problem, even in an `if'.

Tom



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]