Page 1 of 1

C++ expanded template code?

Posted: Wed Sep 17, 2008 6:09 am
by Neo
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?

TIA

Re: C++ expanded template code?

Posted: Sat Oct 25, 2008 12:51 pm
by i586coder
Neo wrote:When C++ templates are used
first of all,

Function templates are special functions that can operate with generic types.

Code: Select all

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

main(){
 int x,y;
 GetMax <int> (x,y);
}
We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:

Code: Select all

template <class T>
class mypair {
    T values [2];
  public:
    mypair (T first, T second)
    {
      values[0]=first; values[1]=second;
    }
};
for more details follow these links:
http://www.cplusplus.com/doc/tutorial/templates.html
http://www.mochima.com/tutorials/STL.html

Cheers,
Ahmad T. Dajani

Re: C++ expanded template code?

Posted: Sat Oct 25, 2008 4:07 pm
by CodeCat
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.

Re: C++ expanded template code?

Posted: Sat Oct 25, 2008 4:49 pm
by Colonel Kernel
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.

Re: C++ expanded template code?

Posted: Tue Oct 28, 2008 5:51 am
by JamesM
AhmadTayseerDajani wrote:
Neo wrote:When C++ templates are used
first of all,

Function templates are special functions that can operate with generic types.

Code: Select all

template <class myType>
myType GetMax (myType a, myType b) {
 return (a>b?a:b);
}

main(){
 int x,y;
 GetMax <int> (x,y);
}
We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:

Code: Select all

template <class T>
class mypair {
    T values [2];
  public:
    mypair (T first, T second)
    {
      values[0]=first; values[1]=second;
    }
};
for more details follow these links:
http://www.cplusplus.com/doc/tutorial/templates.html
http://www.mochima.com/tutorials/STL.html

Cheers,
Ahmad T. Dajani
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.

James

Re: C++ expanded template code?

Posted: Tue Oct 28, 2008 5:59 am
by AJ
Hi,

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.

Cheers,
Adam

Re: C++ expanded template code?

Posted: Thu Oct 30, 2008 3:01 pm
by SlowByte
Does not give you source code, but better than assembly (outputs the intermediate form used by the GCC optimizers):

Code: Select all

gcc -fdump-tree-all test.cpp
Look for files named test.cpp.123.passname in the current directory.