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
C++ expanded template code?
Re: C++ expanded template code?
first of all,Neo wrote:When C++ templates are used
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);
}
Code: Select all
template <class T>
class mypair {
T values [2];
public:
mypair (T first, T second)
{
values[0]=first; values[1]=second;
}
};
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.
but it does make you part of a larger picture.
Re: C++ expanded template code?
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.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: C++ expanded template code?
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.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.
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
Re: C++ expanded template code?
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.AhmadTayseerDajani wrote:first of all,Neo wrote:When C++ templates are used
Function templates are special functions that can operate with generic types.
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 myType> myType GetMax (myType a, myType b) { return (a>b?a:b); } main(){ int x,y; GetMax <int> (x,y); }
for more details follow these links:Code: Select all
template <class T> class mypair { T values [2]; public: mypair (T first, T second) { values[0]=first; values[1]=second; } };
http://www.cplusplus.com/doc/tutorial/templates.html
http://www.mochima.com/tutorials/STL.html
Cheers,
Ahmad T. Dajani
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?
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
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?
Does not give you source code, but better than assembly (outputs the intermediate form used by the GCC optimizers):
Look for files named test.cpp.123.passname in the current directory.
Code: Select all
gcc -fdump-tree-all test.cpp