Is there a way to see the compiler-instantiated code for a function template or a class template in C++?
Assume I have the following piece of code:
template <class T> T add(T a, T b) {
return a + b;
}
When I call:
add<int>(10, 2);
... I would like to see the function that the compiler creates for the int
template specialization.
I am using g++, VC++, and need to know the compiler options to achieve this.
You can definitely see the assembly code generated by the g++ using the "-S" option.
I don't think it is possible to display the "C++" equivalent template code - but I would still want a g++ developer to chime in why - I don't know the architecture of gcc.
When using assembly, you can review the resulting code looking for what resembles your function. As a result of running gcc -S -O1 {yourcode.cpp}, I got this (AMD64, gcc 4.4.4)
_Z3addIiET_S0_S0_:
.LFB2:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
leal (%rsi,%rdi), %eax
ret
.cfi_endproc
Which really is just an int addition (leal).
Now, how to decode the c++ name mangler? there is a utility called c++filt, you paste the canonical (C-equivalent) name and you get the demangled c++ equivalent
qdot@nightfly /dev/shm $ c++filt
_Z3addIiET_S0_S0_
int add<int>(int, int)