编程与软件工程chiark.greenend.org.uk
在 C 中元编程自定义控制结构
Metaprogramming custom control structures in C
2012 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 Simon Tatham 关于用 C 预处理器/宏实现自定义控制结构的页面,核心是像 foreach 这样的宏技巧:不需要用户声明内部临时变量,也不会破坏 break/continue,并且只用标准 C。它把看似古怪的宏元编程变成了有实际用途的容器遍历工具。
为什么经久不衰:它展示了标准 C 宏能做到的巧妙抽象,对需要写容器遍历宏的人至今有参考价值,同时也提醒这种写法强大但应谨慎使用。
HN 评论精选
这套宏技巧虽古怪但标准 C 且实用,能写出干净的 foreach 宏。
有些 C 技巧理论上不错但难以用好,比如用 struct 实现命名参数和默认参数;另一些只在不在乎非 GNU 扩展编译器(如 MSVC)时才实用,比如用 attribute cleanup 做 RAII、用 statement expressions 写更安全的宏。这个控制结构技巧虽然看起来比那些更古怪,却既实用又符合标准 C。如果你在 C 里定义自定义数据容器,它让你能写出一个 foreach 宏:不需要用户声明内部临时变量,不会破坏 break/continue,也不要求在循环首尾写奇怪语法。我强烈推荐读一读。作者还给了自己代码中 HTAB_FOREACH 和 LET_LOOP 宏的例子(文档较少)。
comex查看英文原文
Some C hacks have advantages in theory but hard to use effectively, like named+default parameters via struct. Others are practical, but only if you don't care about compilers without GNU extensions (i.e. MSVC), such as RAII via attribute cleanup and safer macros via statement expressions. This control structure trick, despite being more esoteric looking than any of those, is both useful in practice and standard C. If you're defining a custom data container in C, it will let you write a foreach macro that doesn't require the user to declare any temporaries you need to use, doesn't break the break and continue statements, and doesn't require any weird syntax at the beginning of end of the loop. I highly recommend reading about it. For a (poorly documented) example of actual use from some code I wrote, see HTAB_FOREACH: https://github.com/comex/substitute/blob/master/lib/cbit/htab.h and the LET_LOOP macro it uses: https://github.com/comex/substitute/blob/master/lib/cbit/misc.h
用宏在 C 里模拟 C++ 模板容器,最终不如用 C++。
我曾把 C++ STL 的核心回移到 C,用一些宏魔法给 C 引入模板化容器。最后我学到的是:还不如直接用 C++。
glouwbug查看英文原文
I once back ported the core of the C++ STL to C to introduce templated containers to C with some macro magic. I learned in the end that I’m better off using C++
自定义代码生成/解析比硬用预处理器更省力。
如果你想在 C 里做元编程,与其用 C 预处理器,不如自己写解析/分词并生成自己的宏,那样省事得多。
wudangmonk查看英文原文
If you want to use metaprogramming in C you are better off doing the parsing/tokenizing yourself and creating your own macros than trying to use the C preprocessor, its a lot less work.
这类技巧有趣,但不应被当成写 C 的常规方式。
人们花了太多时间把 C 变成它本不是的东西。当然,这挺有趣,但随后有些人开始认为这就是写 C 的正确方式。
adamnemecek查看英文原文
So much time has been spent on making C something that it’s not. Sure. It’s interesting, but then some people start thinking that this is the way you should be writing C.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选