如何扩展/“预处理” C++模板代码
为了正确调试 C++ 中的复杂宏,我通常对其运行预处理器,以便准确查看生成的代码是什么样子。
是否有类似的方法来“预处理”模板代码?
To properly debug complex macros in C++ I usually run the preprocessor on them in order to see exactly what the resulting code looks like.
Is there a similar way to "preprocess" template code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法(取决于编译器)是在每个编译器步骤之后使用转储。我写了一个小程序:
然后:
这给我带来了一堆文件。看看(在我的例子中)test.cc.218.dfinish:
One way (compiler-dependent) is to use dumping after each compiler step. I wrote a small program:
then:
This get me a bunch of files. Take a look at (in my case) test.cc.218.dfinish:
CLang 编译器具有一个选项
-emit-ast
,它转储用于表示已解析语言的抽象语法树。将表示模板的各种实例。AST 在内存和 xml 版本中表示,因此您可以:
对于大多数代码检查(包括检查所选的重载)我发现实际读取 XML 输出(好吧,通过 grep 查找)就足以满足我的需求。
The CLang compiler features an option
-emit-ast
which dumps the Abstract Syntax Tree used to represent the parsed language. The various instantiations of the template will be represented.The AST is represented both in memory and in xml version, so you can:
For most code inspections (including checking the overloads selected) I have found that actually reading the XML output (well, grepping through it) was sufficient for my needs.
这是一个相当老的问题,但我认为这个领域已经有了显着的改进,但尚未广为人知。
Metashell 可以像某种 gdb 一样用于模板实例化。 (据我所知)这是建立在 clang 工具之上的。
This is a fairly old question, but I think there has been significant improvements in this area that are not so widely known (yet).
Metashell can be used like a sort of gdb for template instantiations. This (as far as I know) builds on clang tooling.