C++ 模板预处理器工具 [英] C++ Template preprocessor tool

查看:27
本文介绍了C++ 模板预处理器工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有编译器或独立的预处理器接收 C++ 文件并运行模板扩展传递,生成具有扩展模板实例化的新 C++ 代码?

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?

我记得 90 年代中期这样的工具,当时模板还是新的和实验性的,预处理器是一种在没有原生模板支持的情况下使用编译器进行模板编程的方法.

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.

我希望在编写 OpenCL 代码时使用这样的工具.OpenCL 是 C++,但不支持模板.我希望我可以编写模板,甚至是简单的模板,例如仅带有整数或布尔参数的模板,并使用一些工具预解析文件并通过查找模板的使用并扩展调用并为我提供新的 C++ 代码OpenCL 编译器可以理解.

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.

替代方案:#define 宏无处不在.. 更丑陋、不安全、效率更低、通用性更差.

The alternative: #define macros everywhere.. uglier, unsafe, less efficient, and less versatile.

推荐答案

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++ 模板预处理器工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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