C++ Template preprocessor tool

邮差的信 提交于 2019-12-18 16:57:06

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1139793/c-template-preprocessor-tool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!