C++ 模板预处理器工具
是否有一个编译器或独立预处理器可以获取 C++ 文件并运行模板扩展过程,从而生成具有扩展模板实例化的新 C++ 代码?
我记得在 90 年代中期就有这样的工具,当时模板还很新,还处于实验阶段,预处理器是一种在没有本机模板支持的情况下使用编译器进行模板编程的方法。
这比宏处理步骤复杂得多,因为它可能需要解析和标记代码以理解上下文。
我希望在编写OpenCL代码时使用这样的工具。 OpenCL 是 C++,但不支持模板。 我希望我可以编写模板,甚至是简单的模板,例如仅使用整数或布尔参数,并使用一些工具来预解析文件并浏览并找到模板的使用并扩展调用,并为我提供新的 C++ 代码OpenCL 编译器可以理解。
即使是非常有限的工具也可能有用,它不需要支持每个模板怪癖,甚至不需要支持多个模块或任何东西。
另一种选择:#define 宏随处可见……更丑陋、不安全、效率较低、通用性较差。
Is there a compiler or standalone preprocessor which takes C++ files and runs a template expansion pass, generating new C++ code with expanded template instantiations?
I remember such a tool in the mid-90s when templates were still new and experimental, and the preprocessor was a way to do template programming with compilers without native template support.
This is a lot more complicated than a macro-processing step since it would likely require parsing and tokenizing the code to understand the contexts.
My hope is to use such a tool when writing OpenCL code. OpenCL is C++, but does not support templates. I'm hoping I can write templates, even simple ones like with integer or bool only arguments, and have some tool pre-parse the file and go through and find the use of the templates and expand the invocations and give me new C++ code that the OpenCL compiler can understand.
Even a very limited tool could be useful, it does not need to support every template quirk, nor even support multiple modules or anything.
The alternative: #define
macros everywhere.. uglier, unsafe, less efficient, and less versatile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Comeau C++ 可以将 C++“编译”为 C。这似乎接近您的目标,因为 OpenCL 没有支持 C++——它更接近 C。
Comeau C++ can "compile" C++ to C. This would seem to be close to your goal, as OpenCL does not support C++ – it's much closer to C.
C++ Insights (https://cppinsights.io/) 能够做到这一点(更普遍地扩展“高-level”语法糖 C++ 构造。它基于 Clang,因此它对代码有尽可能好的理解,并支持最新标准。
例如,它将扩展
为
C++ Insights (https://cppinsights.io/) is able to do this (and more generally expansion of "high-level" syntax-sugary C++ constructs. It is based on Clang so it has an understanding of the code which is as good as possible, and supports latest standards.
It will for instance expand
into
没有这样的工具 - 模板是语言的一部分,而不是一些预处理器通道 - 它们由编译器处理,就像其他代码一样。
There is no such tool - templates are part of the language, not some pre-processor pass - they are processed by the compiler, just like other code.