A macro skill learned

xiaoxiao2021-03-06  70

I recently saw an article in Mengyan introduced a macro stunning skill of C language and I feel very valuable. Macro is as follows:

#define return switch (Exception_Stack = / EXCEPTION_STACK-> Prev, 0) Default: Return

It is aware that this is a macro retturn that implements a Return feature in C, but it guarantees that exception_stack = exception_stack-> prev must be performed before returnit, and Return will be replaced with this return macro . Do you feel very ordinary? Well, let's try other practices. such as:

#define return_1 f (Exception_stack = / EXCEPTION_STACK-> Prev, 1) Return

Is there any problem in the case of this RETURN?

IF (...) Return_1 1; Else {// Do Something ....} Let's take a look at the code after the macro:

if (...) IF (Exception_stack = Exception_Stack-> Prev, 1) Return 1; Else {// do something ....} is obvious, our Else will be with the IF in the macro instead of the first IF matches, is this what I want? Oh, of course not. That look at another one:

#define return_2 return eXception_stack = / exception_stack-> prev,

If there is such a function

Void foo () {....

Return;} This non-parameter RETURN usage is legal and quite common, is it replaced with RETURN_2 rows? Oh, the fools of small do not die are all speaking. Of course, you can define this:

#define return_3 (_x) do {if (Exception_stack = / EXCEPTION_STACK-> Prev, 1) Return _x} while (0)

It can be used in both cases, but when using it, it is not to bring a bracket. There is a saying: "If it is not necessary, don't break the customary habit." In fact, there is still a macro definition with the above RETURN equation:

#define return_4 for (Exception_stack = / EXCEPTION_STACK-> prev ;; Return

This usage of Switch and For is indeed known, and if you use well, you can define a safe macro.

转载请注明原文地址:https://www.9cbs.com/read-91977.html

New Post(0)