将函数转换为宏 [英] Converting a function to a macro

查看:185
本文介绍了将函数转换为宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

今天我正在接近Code Project的精彩社区,关于用C / C ++重写函数作为宏的问题。



我目前正在学习C(和C ++),作为一项帮助我学习更多内容的活动,我正在编写一个程序,它将获取一个文件并读取它,然后将数据显示在一个对用户有用的方式。该项目可能相当无用,但到目前为止,已经值得。 (我已经能够学习文件I / O,并且我可以自由学习指针的基础知识,这样我就可以更多地了解文件I / O如何在C / C ++中工作)



现在,为了解决这个问题:

我写了一个函数,它接受一个字符串,并根据一个令牌爆炸它(是的) ,我知道有tokstr,但同样,这是为了学习)。我最近一直在阅读有关宏的内容,我对它们的看法不一。



一方面,人们说是的,使用宏[如果你计划重用代码],另一方面,人们说不,在C ++中应该避免使用宏,所以我有点想弄清楚,我是否应该花时间编写宏来替换这个函数?



其次,我想知道(即使我不打算为这个函数编写宏),我应该避免在宏中做什么?我还读过你应该在你的宏中包围代码,用do / while循环,是否有特定原因?



我为此道歉很长的问题,但希望我能得到一些帮助。



圣帕特里克节快乐,



Gigabyte Giant

Hello all,
Today I am approaching the wonderful community of Code Project, with a question about rewriting functions as macros in C/C++.

I'm currently learning C (and C++), and, as an activity to help me learn more, I'm writing a program that'll take a file, and read it, and then display the data in a way that is useful to the user. The project might be fairly useless, but so far, it has been worth it. (I've been able to learn File I/O, and I took the liberty of learning the basics of pointers, so that I would understand more about how File I/O works in C/C++)

Now, to get to the point of this question:
I wrote a function, that takes a string, and "explodes" it, based on a token (yes, I know there is tokstr, but again, this is for learning). I've been reading about macros lately, and I've gotten mixed-opinions about them.

On one hand, people say "yes, use macros [if you plan on reusing the code]", and on the other, people say "no, macros should be avoided in C++", so I'm sort of trying to figure out, should I invest time in writing a macro to replace this function?

Secondly, I'd like to know (even if I'm not going to write a macro for this function), what should I refrain from doing in a macro? I've also read that you should surround code in your macro(s), with a do/while loop, is there a specific reason?

I do apologize for the long question, but hopefully I'll be able to get some help.

Happy St. Patrick's Day,

Gigabyte Giant

推荐答案

为什么宏是好的?



1.避免重复打字(减少出错的风险)。



示例:因此,MFC,boost和ATL / WTL使用宏。



2.使用完全不同的行为来编写现有代码。



示例:模拟测试设施;交换新的定义和删除堆调试。



3.生成代码 - 源代码不适合手工编辑。



为什么宏不好?



1.对调试产生不利影响。

a。单步通过宏扩展到十几行。

b。在宏定义上设置一个断点。



2.隐藏 - 宏可以隐藏你的东西 - 使调试和代码审查更加困难。



示例:#define true false



3.欺骗现有代码,使用截然不同的行为进行编译。



4.宏不限于命名空间。



返回当天 - 内联函数之前 - 定义的函数作为一个宏是一种内联代码而不是普通函数调用的方法。使用__inline关键字,这个宏的使用已经过时。



摘要



仅使用宏如果绝对需要并且只有在没有其他机制的情况下。相反,对类型和常量/文字使用const,enum和typedef。



您将函数重写为宏的兴趣作为学习练习具有很大的价值但没有更多。一旦你开始使用其他宏定义宏,你也会发现一些惊喜。



完成学习宏之后,你最终可以使用C ++模板 - 这是比宏更强大 - 但可以限制在命名空间。



祝你好运。
Why macros are good?

1. avoid repetitive typing (less risk of mistakes).

Examples: MFC, boost and ATL/WTL use macros for this reason.

2. trick existing code to be compiled with wildly different behavior.

Examples: Mock facilities for testing; swapping out definitions of new and delete for heap debugging.

3. generated code - source code not meant for hand-editing.

Why macros are bad?

1. Adversely impacts debugging.
a. single-step through a macro that expands to a dozen lines.
b. set a break point on a macro definition.

2. Hiding - macros can hide things from you - to make debugging and code review more difficult.

Example: #define true false

3. Trick existing code to be compiled with wildly different behavior.

4. Macros are not confined to a namespace.

Back in the day - before inline functions - a function defined as a macro was a way to have inline code instead of a plain function call. With the __inline keyword, this use of a macro is obsolete.

Summary

Use a macro only if absolutely needed and only if there is no other mechanism. Instead, use const, enum, and typedef for types and constants / literals.

Your interest in re-writing a function as a macro has much value as a learning exercise but no more. You will also find a few surprises once you start defining macros using other macros.

After you're done learning macros, you can eventually play with C++ templates - which are more more powerful than macros - but can be confined to a namespace.

Good luck.


这篇关于将函数转换为宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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