代码之家  ›  专栏  ›  技术社区  ›  Ganesh M

查看函数模板实例化

  •  4
  • Ganesh M  · 技术社区  · 16 年前

    我有一个简单的函数模板:

    #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;
    }
    

    上面的示例将生成2个函数模板实例化,一个用于 还有一个 长的 . 是否有任何g++选项可用于查看函数模板实例化?

    2 回复  |  直到 16 年前
        1
  •  5
  •   Adam Rosenfield    16 年前

    你可以使用 nm 计划(部分) binutils )查看程序使用的符号列表。例如:

    $ 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
    

    我不知道为什么每一本都有两份,一份有一份 .eh 纳米 支持 -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
    

    如果不支持该选项,则可以使用 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
    

    所以,你可以看到 GetMax 被实例化为 int 和 long 分别地

        2
  •  1
  •   Splat    16 年前

    这个命令行对我有用:

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

    这可能比你要寻找的要远。

    推荐文章