C ++将功能实现为宏的优点/缺点 [英] C++ Advantages/Disadvantages of Implementing functions as macros

查看:73
本文介绍了C ++将功能实现为宏的优点/缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将业务逻辑功能实现为宏是一个好主意吗?

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

我继承了一些旧的c ++代码,发现很多业务逻辑功能都是通过长的隐秘宏实现的.

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?

哪种逻辑最适合宏?

这是代码中的一个简单示例

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;\

}

推荐答案

有效的C ++

constsenumsinlines优先于#defines

Meyers专门指代编写宏而不是编写函数的做法:

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

#define指令的另一个常见(错误)用法是使用它来 实现看起来像函数但不会产生开销的宏 函数调用.

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.

要专门回答您的问题:

  • 一些程序员编写宏而不是函数,以避免感觉到的函数调用开销-这是一种可疑的做法,通常没有多少可衡量的好处.
  • 程序员曾经使用预处理器来生成样板代码(也许仍然会这样做).在现代C ++中,对于条件编译,应将处理器的使用限制为#include和(也许)为#ifdef/#ifndef.
  • 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.

这篇关于C ++将功能实现为宏的优点/缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆