tags:

views:

102

answers:

2

I have a simple function template:

#include <iostream>
using namespace std;

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

int main () {
  cout << GetMax<int>(5, 6) << endl;
  cout << GetMax<long>(10, 5) << endl;
  return 0;
}

The above example will generate 2 function template instantiations, one for int and another for long. Is there any g++ option to view the function template instantiations?

+1  A: 

If you have the linker generate a map file, you can see the names of all generated symbols. Some parsing with grep/perl may get you what you want. The names will be mangled.

This command line worked for me:

g++ -o test -Wl,-map,test.map test.cpp

test.map will be generated.

This might be further down the pipeline than you're looking for though.

Splat
Another option is the nm program, which is part of binutils and thus should be available on any platform in which GCC is available.
Dan Olson
+3  A: 

You can use the nm program (part of binutils) to see the list of symbols used by your program. For example:

$ g++ test.cc -o test
$ nm test | grep GetMax
00002ef0 T __Z6GetMaxIiET_S0_S0_
00002f5c T __Z6GetMaxIiET_S0_S0_.eh
00002f17 T __Z6GetMaxIlET_S0_S0_
00002f80 T __Z6GetMaxIlET_S0_S0_.eh

I don't know why each one has two copies, one with a .eh suffix, but otherwise you can tell that this particular function was instantiated twice. If you version of nm supports the -C/--demangle flag, you can use that to get readable names:

$ nm --demangle test | grep GetMax
00002ef0 T int GetMax<int>(int, int)
00002f5c T _Z6GetMaxIiET_S0_S0_.eh
00002f17 T long GetMax<long>(long, long)
00002f80 T _Z6GetMaxIlET_S0_S0_.eh

If that option isn't supported, you can use c++filt to demangle them:

$ nm test | grep GetMax | c++filt
00002ef0 T int GetMax<int>(int, int)
00002f5c T __Z6GetMaxIiET_S0_S0_.eh
00002f17 T long GetMax<long>(long, long)
00002f80 T __Z6GetMaxIlET_S0_S0_.eh

So, you can see that GetMax was instantiated with int and long respectively.

Adam Rosenfield