C++ Template preprocessor tool

cc-preprocessormacrosopencltemplates

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.

Best Answer

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.