【发布时间】:2012-05-29 11:31:48
【问题描述】:
考虑以下代码:
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++ 不接受此代码时,对我有什么好处?
谢谢! 内森
【问题讨论】:
-
更改为
this->doSomething()(或Base<T>::doSomething()),因为doSomething()取决于T的类型。 -
@hmjd:这很有趣..你能简要描述一下添加
this->有什么帮助吗? -
查看this question 并在其中回答。
-
@Asha,
this->告诉编译器名称是一个成员,因此名称查找会延迟到第 2 阶段(实例化),此时基类的类型已知并且其成员的声明是可见
标签: c++ templates g++ compiler-options