Large Macros C. What is the use?

I worked with a large code base written mainly by programmers who no longer work in the company. One of the programmers, apparently, occupied a special place in his heart for very long macros. The only advantage that I can see when using macros is the ability to write functions that do not need to be passed in all their parameters (which is recommended in the best practice guide that I read). Other than that, I see no advantage over the built-in function.

Some macros are so complex that it’s hard for me to imagine, someone even writes them. I tried to create in that spirit, and it was a nightmare. Debugging is extremely difficult because there are N + lines of code in 1 in the debugger (for example, in this large block of code there is segfault. Good luck!). I had to actually pull out the macro and run it unscaled to debug it. The only way I could see that the person wrote them was to automatically generate them from code written in the function after he debugged it (or, being smarter than me, and wrote it for the first time, which is always possible, I think )

Am I missing something? I'm crazy? Are there any debugging tricks that I don't know about? Please fill me up. I would love to hear from amateur lovers. :)

+5
source share
11 answers

For me, the best use of macros is code compression and error reduction. The disadvantage is obviously debugging, so they should be used with caution.

I am inclined to think that if the resulting code is not an order of magnitude smaller and less error-prone (which means that macros take care of some accounting details), then it was not worth it.

In C ++, many of them, like this, can be replaced with templates, but not all. A simple example of macros that are useful in MFC event handler macros - without them, creating event tables would be much more complicated, and the code you would have to write (and read) would be much more complicated.

+8
source

, , , , . , , , .

, , , , , , .

+4

. , , ++ inline, .

, . :

#define max(a, b) ((a)>(b)?(a):(b))

:

max(i++, j++)

. ,

#define PLANETS 8
#define SOCCER_MIDDLE_RIGHT 8

, "8", .

+3

, .

-, . , inline , inlining -.

-, C ++. " , ", , . , .

, , (, __FILE__ __LINE__ ) , (< href= "http://www.boost.org/doc/libs/1_39_0/libs/preprocessor/doc/index.html" rel= "nofollow noreferrer" > Boost.Preprocessor, . Boost.ScopeExit ), , , , .

+3

, , , , .

( , ) LOC. /, , .

+2

- ...

, , , ... , .

+1

C.

x-, , , // .

, , .

, ( X ..) .

C .

+1

. , . , ( - ?).

0

, (, , ) . "" - . , ++ C, , . , ( , ). ! , factory, ... .

, , , , , . , , -. -, . , . , , , - .

/. , , .

, , , . , . , . , , , . , . .

0

, , . , // , . , .

, , ( ) .

0

C89 . ( ), .

, C99 1999 , ; , C99. (, MS) . C99, , .

C89, , (, , ), .

Another thing is that the macro version effectively forces the function to actually be inline. The C99 keyword inlineis just a hint to the compiler, and the compiler can still decide to create one instance of the function code that is linked as a function other than the built-in one. (One compiler that I still use will do this if the function is not trivial and returns void).

0
source

All Articles