2016-12-30 47 views
22

当我通过模板函数作为基类的一个模板参数,链接器抱怨说,它不能链接函数:错误消息“未定义参考作为模板参数传递模板函数”

#include <stdio.h> 

template<int I> inline int identity() {return I;} 
//template<> inline int identity<10>() {return 20;} 

template<int (*fn)()> 
class Base { 
public: 
    int f() { 
     return fn(); 
    } 
}; 

template<int Val> 
class Derived : public Base<identity<10> > { 
public: 
    int f2() { 
     return f(); 
    } 
}; 

int main(int argc, char **argv) { 
    Derived<10> o; 
    printf("result: %d\n", o.f2()); 
    return 0; 
} 

结果:

$ g++ -o test2 test2.cpp && ./test2 
/tmp/ccahIuzY.o: In function `Base<&(int identity<10>())>::f()': 
test2.cpp:(.text._ZN4BaseIXadL_Z8identityILi10EEivEEE1fEv[_ZN4BaseIXadL_Z8identityILi10EEivEEE1fEv]+0xd): undefined reference to `int identity<10>()' 
collect2: error: ld returned 1 exit status 

如果我注释掉专业化,那么代码将按照预期编译和链接。另外,如果我继承Base<identity<Val> >而不是Base<identity<10> >,则代码按我的预期工作。

试一下:http://coliru.stacked-crooked.com/a/9fd1c3aae847aaf7

我怎么错过?

+3

这个问题似乎是一个gcc的错误:它编译和使用铛和ICC链接确定。顺便说一句,名称_identity()_通常用于转换,其结果与参数完全相同。 –

+0

@DietmarKühl好吧,'身份()'返回'X'。 :-) – melpomene

+2

解决方法:'派生类:public Base (identity <10>)>'。 [live demo](http://melpon.org/wandbox/permlink/E4aRHqcZaac7vd3C) –

回答

19

看来问题是gcc的一个错误:代码编译和铿锵,国际商会,以及EDG前端的链接。一个潜在的变通办法不改变任何用途将是使用,而不是函数的类模板identity的:

template<int I> 
struct identity { 
    operator int() { return I; } 
}; 

template<typename fn> 
class Base { 
public: 
    int f() { 
     return fn(); 
    } 
}; 
+0

不制作函数模板'inline'也可以。链接器是否应该删除重复的模板实例? – Etherealone