views:

1070

answers:

4

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.

A: 

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.

anon
Yes.. such a tool would indeed need to parse the C++. That's why it's more complicated than a macro preprocessor.
SPWorley
Well, like I say, no such thing exists to my knowledge. I can't see how it could process the templates and leave the "non-template" code untouched.
anon
@Neil: the very first c++ compilers simply converted c++ code to valid c and compiled it using a c compiler. Certainly this is no longer the case, but used to be fairly usual.
Evan Teran
@Evan I know - I was a user of cfront from the E release onwards. However, the original question said that he wanted to expand the templates but leave the the other C++ code alone, producing a new C++ program, which cfront (and comeau) can't do. It now turns out he wants something different - such is life.
anon
@Evan Teran: The very first C++ compilers didn't compile C++ as we know it. They compiled the very first versions of C++, which was vastly different. ;)
jalf
A: 

Why not take a look at GCC's C++ compiler? I'm sure with a bit of effort you can hijack the compiling process and simply dump the "expanded" templates into their own code files. However, writing this yourself would be fairly simply, I would imagine. I don't see why you need a "full" compiler in order to accomplish this at all. From my experience, templates only are aware of the context in instances where the compiler wants to give you an error. If you validate your template code before-hand with a real compiler, then run it though a "search/replace" tool, you will get valid code; I'm sure.

nlaq
You might as well go with http://www.gccxml.org if you're going this route; they've already done the hard work of ripping out GCC's front-end.
ephemient
If by "a bit of effort" you mean "more effort than writing a new architecture backend," then sure. Unfortunately you can't actually expand templates without type info which means you do not do it as a preprocessor, you do it part way through AST generation. The net result is that by the time C++ templates are expanding you are nowhere near the original code and you need actual backend that can translate from ASTs to C (thought it can be transitive with anything else that ASTs can go to, so in GCC you could do it from GIMPLE or TreeSSA).
Louis Gerbarg
+4  A: 
ephemient
Yes! This is the tool I used 15 years ago for the same reason! Excellent, thanks.
SPWorley
"for the same reason" -- you had OpenCL 15 years ago? Wow! ;-)
ephemient
A: 

Use Cheetah to automate it. It is a good tool to know anyway.

Chad Brewbaker