When C++ templates are used, is it possible to view the expanded C++ code for the relevant data types (the ones that are used in the code)?
If yes, is there some G++ compiler option that can be used to view this? (Couldn't find any so far )
If no is this not possible because the code is expanded into object code? Or something else altogether?
Expanding a template is really not much more than replacing every templated type with the actual type used. I don't think you'd need a compiler for that.
CodeCat wrote:Expanding a template is really not much more than replacing every templated type with the actual type used. I don't think you'd need a compiler for that.
If you're using a compiler like g++ or Visual C++ that fully supports templates, you can have template-template parameters and partial specialization, which allows you to create recursively-defined templates and other crazy stuff. At that point, the template system becomes a programming language in its own right. It's as painful to expand such crazy template code manually as it would be to evaluate a large functional program by hand.
Unfortunately, I don't know of any way to get the compiler to spit out non-templatized C++ source code. I think the only thing you can do is disassemble the generated code.
Top three reasons why my OS project died:
Too much overtime at work
Got married
My brain got stuck in an infinite loop while trying to design the memory manager
Read the OP's question, then try and work out whether you answered that question, or tried to give him a beginner's introduction to C++ templates. The question was specific.
OP: I really wish there were a way to do what you ask with GCC. Unfortunately I don't know of one - my own technique is to disassemble the generated code and run c++filt on some of the template symbols to see what instances it's created.
Depending on your reasons for wanting this, it may be useful to know that some IDE's also provide varying degrees of inlellisense with this (for example, eclipse seems to make relevant suggestions depending on the template data type). I just wonder if there is an IDE out there somewhere that can perform more of a thorough template class expansion...
Sorry if this isn't much help, but just a thought.