简体   繁体   English

查看功能模板实例化

[英]View function template instantiations

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 . 上面的例子将生成2个函数模板实例,一个用于int ,另一个用于long Is there any g++ option to view the function template instantiations? 是否有任何g ++选项可以查看函数模板实例化?

You can use the nm program (part of binutils ) to see the list of symbols used by your program. 您可以使用nm程序( binutils的一部分)查看程序使用的符号列表。 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. 我不知道为什么每个都有两个副本,一个带有.eh后缀,但是你可以告诉我这个特定的函数被实例化了两次。 If you version of nm supports the -C/--demangle flag, you can use that to get readable names: 如果您的nm版本支持-C/--demangle标志,您可以使用它来获取可读名称:

$ 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: 如果不支持该选项,则可以使用c++filt对它们进行解码:

$ 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. 因此,您可以看到GetMax分别使用intlong实例化。

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. 一些使用grep / perl解析可能会得到你想要的。 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. 将生成test.map。

This might be further down the pipeline than you're looking for though. 这可能比你想要的更进一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM