宏:有什么好处?
保罗·格雷厄姆写道:
例如,类型似乎是 取之不尽用之不竭的研究资源 论文,尽管事实上静态 打字似乎排除了真正的宏—— 在我看来,没有它,就没有 语言是值得使用的。
宏有什么大不了的?我没有花很多时间使用它们,但从我使用过的遗留 C/C++ 来看,它们似乎主要在模板/泛型存在之前用作 hack。
很难想象这
DECLARELIST(StrList, string);
StrList slist;
比
List<String> slist;
我错过了什么更好吗?
然后是伪函数的用法,例如 MAKEPOINTS :
POINTS MAKEPOINTS(
DWORD dwValue
);
为什么不将其定义为函数呢?这是一些优化,可以避免代码重复而不会增加另一个堆栈帧的开销吗?
然后还有涉及 GOTO 的棘手控制流问题,这似乎具有可疑的价值。
宏有什么伟大之处?它们的类型安全性较低(在 C 和 C++ 中)(对吗?)。为什么保罗·格雷厄姆在没有它们的情况下不进行编程呢?
Paul Graham writes:
For example, types seem to be an
inexhaustible source of research
papers, despite the fact that static
typing seems to preclude true macros--
without which, in my opinion, no
language is worth using.
What's the big deal with macros? I haven't spent a whole lot of time with them, but from the legacy C/C++ I've worked with they appear to be mostly used as a hack before templates/generics existed.
It's hard to imagine that
DECLARELIST(StrList, string);
StrList slist;
is somehow preferable to
List<String> slist;
Am I missing something?
Then there's the usage as a pseudo-function, like MAKEPOINTS:
POINTS MAKEPOINTS(
DWORD dwValue
);
Why not define it as a function instead? Is this some optimization, where you avoid code duplication without having the added overhead of another stack frame?
Then there's also tricky control flow things involving GOTO, which seem to be of dubious value.
What's so great about macros? They're less type safe (in C and C++) (right?). Why won't Paul Graham program without them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LISP 宏是一种完全不同的野兽。 C/C++ 宏只能使用极其基本的语言将一段文本替换为另一段文本。而 LISP 程序(“读取”后)是 LISP 数据结构,因此可以使用整个语言进行操作。
有了这样的宏,您就可以(假设您是一个非常聪明的黑客)极大地扩展该语言,并且每个人都可以相对轻松地使用它,因为您是使用宏来做到这一点的。以 Common Lisp 对象系统为例。从本质上讲,该语言与对象没有任何相似之处。它完全是用语言本身实现的,包括一个相对简单的使用语法——使用宏。
当然,当语言具有您想要的大多数内置功能时,宏就没那么必要了。 OTOH,LISP 粉丝认为,具有足够强大的元编程功能(宏)的足够简单的语言 (LISP) 更好,因为可以将新概念合并到语言中,而无需更改规范或工作实现。但宏使用最引人注目的例子是 DSL 领域。 Ruby on Rails 和其他工具每天都在展示 DSL 的有用性。是的,Ruby 没有宏,它只是利用了 Ruby 语法的可弯曲程度。在其他语言中,或者当 Ruby 的语法不够灵活时,您需要宏或成熟的解析器/解释器来实现复杂的 DSL。
LISP macros are an entirely different beast. C/C++ macros can merely replace a piece of text with abother piece of text using an extremely basic language. Whereas a LISP program is (after "reading") is a LISP data structure and can therefore be manipulated using the whole language.
With such macros, you could (given you're a really clever hacker) vastly extend the language and everybody could use it relatively easily, since you did it with macros. Take for example the the Common Lisp Object System. At its core, the language has nothing even remotely like objects. It is entirely implemented in the language itself, including a relatively simple syntax for use - using macros.
Of course macros are less necessary when the language has most things you'd every want built-in. OTOH, the LISP fans are of the opinion that a sufficiently simple language (LISP) with sufficiently powerful metaprogramming capabilities (macros) is better since new concepts can be incorporated into the language without changing the spec or working implementations. But the most compelling example for macro usage is the DSL area. Ruby on Rails and others show every day how useful DSLs can be. Yes, Ruby doesn't have macros, it just exploits how much Ruby syntax can be bent. In other languages, or when even Ruby's syntax isn't flexible enough, you need macros or a fully-blown parser/interpreter to implement a complex DSL.
在 C/C++ 中,宏实际上只对两件事有用,并且通常应该是最后的工具(如果您可以在不使用宏的情况下完成某件事,那就这样做)。
几乎从不使用宏作为函数。
您还必须认识到 LISP 宏不是 C/C++ 宏。
Macros are really only good for two things in C/C++, and should generally be the tool of last resort (if you can accomplish something without using macros, do so).
It's almost never to use a macro as a function.
You also have to realize that LISP macros are not C/C++ macros.