Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages/disadvantages of implementing functions as macros in C++

Tags:

c++

macros

Is it a good idea to implement business logic functions as macros?

I inherited some legacy C++ code and I find a whole lot of business logic functions are implemented as long cryptic macros.

Is there an advantage of macros over functions? What is the general rationale behind the use of macros?

What kind of logic is best suited for macros?

Here is a simple sample from code

#define INSERT_VALUES(IN,ID,EO)     {\
    double evaluationOutput = EO;\
    int controls = 0;\
    int input_controls = m_input_controls[IN];\
    if(m_value_list[IN].ShouldProcess())\
        {\
        evaluationOutput = m_evaluationOutput[IN];\
        controls = m_controls[IN];\
        }\
    VALUE_EXIST(evaluationOutput,controls,input_controls,IN,ID,Adj);\
    m_evaluationOutput[IN] = controls > 0 ? evaluationOutput : 0.0;\
    m_controls[IN] = controls;\
    m_input_controls[IN] = input_controls;\                                 
}
like image 868
bsobaid Avatar asked Dec 02 '22 23:12

bsobaid


1 Answers

In Effective C++, Scott Meyers notes in Item 2:

Prefer consts, enums, and inlines to #defines

Specifically referring to the practice of writing macros instead of functions, Meyers says:

Another common (mis)use of the #define directive is using it to implement macros that look like functions but don't incur the overhead of a function call.

Macros like this have so many drawbacks, just thinking about them is painful.

Fortunately, you don't have to put up with this nonsense. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using a template for an inline function.

[Real functions] obey scope and access rules. For example, it makes perfect sense to talk about an inline function that is private to a class. In general, there's just no way to do that with a macro.

To answer your questions specifically:

  • Some programmers write macros instead of functions to avoid the perceived overhead of a function call -- a dubious practice that often has little measurable benefit.
  • Programmers used to use the preprocessor to generate boilerplate code (and perhaps still do). In modern C++, use of the processor should be limited to #include and (maybe) #ifdef/#ifndef for conditional compilation.

As Meyers notes in closing:

It's not yet time to retire the preprocessor, but you should definitely give it long and frequent vacations.

like image 187
Gnawme Avatar answered May 18 '23 14:05

Gnawme