Why are preprocessor macros evil and what are the alternatives?

前端 未结 8 2026
面向向阳花
面向向阳花 2020-11-22 02:46

I have always asked this but I have never received a really good answer; I think that almost any programmer before even writing the first \"Hello World\" had encountered a p

8条回答
  •  一生所求
    2020-11-22 02:52

    Macros in C/C++ can serve as an important tool for version control. Same code can be delivered to two clients with a minor configuration of Macros. I use things like

    #define IBM_AS_CLIENT
    #ifdef IBM_AS_CLIENT 
      #define SOME_VALUE1 X
      #define SOME_VALUE2 Y
    #else
      #define SOME_VALUE1 P
      #define SOME_VALUE2 Q
    #endif
    

    This kind of functionality is not so easily possible without macros. Macros are actually a great Software Configuration Management Tool and not just a way to create shortcuts for reuse of code. Defining functions for the purpose of reusability in macros can definitely create problems.

提交回复
热议问题