为什么模板函数不会出现在LLVM-IR中?

Yas*_*ari 1 c++ llvm llvm-clang llvm-ir

为什么模板函数在LLVM-IR中不显示,如果没有调用函数,从c ++代码发出LLVM IR,不像其他类型的函数(int,float ...)将出现在llvm ir示例中:以下函数func1不会在llvm ir中显示

template <class tmp>
tmp func1 () {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是这个功能func2总是在llvm ir中显示

int func2 () {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

Gui*_*cot 6

这是因为您的模板不是函数:它们是函数模板.在用参数实例化之前,它们不是发现.例如,请使用以下代码:

template<typename T>
T foo() { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

那也不会输出任何代码.

但另一方面:

template<typename T>
T foo() { /* ... */ }

int test() {
    return foo<int>();
}
Run Code Online (Sandbox Code Playgroud)

将输出的代码都testfoo<int>.

您也可以手动实例化这样的模板:

template int foo<int>();
Run Code Online (Sandbox Code Playgroud)