C++ expanded template code?

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

C++ expanded template code?

Post 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
Only Human
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: C++ expanded template code?

Post 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
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: C++ expanded template code?

Post 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.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: C++ expanded template code?

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: C++ expanded template code?

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: C++ expanded template code?

Post 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
SlowByte
Posts: 2
Joined: Thu Oct 30, 2008 2:58 pm

Re: C++ expanded template code?

Post 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.
Post Reply