当我通过模板函数作为基类的一个模板参数,链接器抱怨说,它不能链接函数:错误消息“未定义参考作为模板参数传递模板函数”
#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
我怎么错过?
这个问题似乎是一个gcc的错误:它编译和使用铛和ICC链接确定。顺便说一句,名称_identity()_通常用于转换,其结果与参数完全相同。 –
@DietmarKühl好吧,'身份()'返回'X'。 :-) –
melpomene
解决方法:'派生类:public Base(identity <10>)>'。 [live demo](http://melpon.org/wandbox/permlink/E4aRHqcZaac7vd3C) –