【发布时间】:2013-01-14 13:05:52
【问题描述】:
假设我有以下类层次结构:
template< class T >
class TestBase {
public:
virtual T const & do_foo() = 0;
};
template< class T >
class TestDerived : public virtual TestBase< T > {
public:
virtual int do_bar() {
return do_foo() + 1;
}
};
GCC 吐出以下内容:
error: there are no arguments to ‘do_foo’ that depend on a template parameter, so a declaration of ‘do_foo’ must be available [-fpermissive]
note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
现在,如果我将其更改为从从已知类型实例化的 TestBase 派生(例如 class TestDerived : public virtual TestBase< int >,此 sn-p 编译得很好,因此问题显然与编译时未知的基类类型有关。但由于我没有在任何地方实例化任何一个类的对象,我不明白这为什么重要。
基本上,如果不求助于-fpermissive,我将如何解决错误?
【问题讨论】:
-
不应该是
virtual T do_bar()inTestDerived吗? -
@BartekBanachewicz - 我认为没有必要,T 可以是任何类型,例如
decltype(do_foo() + 1)是一个 int(或者我认为可以隐式转换为一个 int)。 -
哦,好的。不过,它肯定会触发实例化。