考虑以下代码:
template<class T>
class Base {
public:
void doSomething(){}
};
template<class T>
class Derived : public Base<T> {
public:
void doMore() {
doSomething(); //Affected line
}
};
在用“受影响的行”注释的行中,g++ (4.7) 说:
test.cc:11:16: error: there are no arguments to ‘doSomething’ that depend on a template parameter, so a declaration of ‘doSomething’ must be available [-fpermissive]
现在我想知道:
- 如果模板参数 T 不存在,则不会发生此错误。有什么不同?
- g++ 显然能够解决这个问题(如果我添加 -fpermissive 它编译得很好)。我假设 g++ 试图为我作为“用户”(程序员)提供最佳体验。当 g++ 不接受此代码时,对我有什么好处?
谢谢!弥敦道