【发布时间】:2015-07-12 17:19:34
【问题描述】:
这听起来有点奇怪,我可能不得不在某些时候重构我的代码,但我需要使用模板函数生成纯虚拟基类方法。使用 C++11(可变参数模板?)是否可行?
例子:
struct I
{
virtual void foo(int) = 0;
virtual void foo(float) = 0;
};
struct S : public I
{
template<typename T>
void foo(T t) { /*do the same symbolic stuff on t*/ }
};
int main()
{
S s;
s.foo(0);
s.foo(0.0f);
return 0;
}
给出以下错误(clang):
main.cpp:65:7: error: variable type 'S' is an abstract class
S s;
^
main.cpp:53:18: note: unimplemented pure virtual method 'foo' in 'S'
virtual void foo(int) = 0;
^
main.cpp:54:18: note: unimplemented pure virtual method 'foo' in 'S'
virtual void foo(float) = 0;
^
1 error generated.
【问题讨论】:
标签: c++ templates c++11 pure-virtual