Fer*_*enc 22 c++ inheritance gcc templates template-inheritance
当我将模板函数作为基类的模板参数传递时,链接器会抱怨它无法链接该函数:
#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;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
$ 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
Run Code Online (Sandbox Code Playgroud)
如果我注释掉专业化,那么代码将按预期编译和链接.此外,如果我继承Base<identity<Val> >而不是Base<identity<10> >,代码按照我的预期工作.
试试这里:http://coliru.stacked-crooked.com/a/9fd1c3aae847aaf7
我错过了什么?
Die*_*ühl 19
似乎问题是gcc错误:代码编译并链接clang,icc和EDG前端.不改变任何用途的潜在解决方法是使用类模板identity而不是函数:
template<int I>
struct identity {
operator int() { return I; }
};
template<typename fn>
class Base {
public:
int f() {
return fn();
}
};
Run Code Online (Sandbox Code Playgroud)
将其提升为typedef会使其编译,即
typedef Base< identity<10> > base10;
Run Code Online (Sandbox Code Playgroud)
我不太确定为什么在类定义中直接执行它不起作用.
http://coliru.stacked-crooked.com/a/f00b4f4d1c43c2b0