-fpermissive オプションなしではコンパイルできなくなった C++ コードがいくつかあります。共有できない適切なコードですが、問題を示す簡単なテスト ケースを抽出できたと思います。g++ からの出力は次のとおりです。
template_eg.cpp: In instantiation of 'void Special_List<T>::do_other_stuff(T*) [with T = int]':
template_eg.cpp:27:35: required from here
template_eg.cpp:18:25: error: 'next' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
template_eg.cpp:18:25: note: declarations in dependent base 'List<int>' are not found by unqualified lookup
template_eg.cpp:18:25: note: use 'this->next' instead
したがって、問題を生成するコードは次のとおりです。
template<class T> class List
{
public:
void next(T*){
cout<<"Doing some stuff"<<endl;
}
};
template<class T> class Special_List: public List<T>
{
public:
void do_other_stuff(T* item){
next(item);
}
};
int main(int argc, char *argv[])
{
Special_List<int> b;
int test_int = 3;
b.do_other_stuff(&test_int);
}
コードを修正して再度コンパイルする方法を見つけようとしているわけではありません。これは単に next(item) を this->next(item) に変更するだけの問題です。この変更が必要な理由をよりよく理解しようとしています。このページで説明を見つけました: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html その説明は役に立ちましたが、まだいくつか質問があります。私の関数が T* (型 T へのポインタ) を取るという事実は、それをテンプレート引数に依存させるべきではありません。私自身の言い回しでは、コンパイラ (gcc 4.7) は、next() 関数が基本クラス List にあることを認識できるべきではありませんか? そのようなすべての呼び出しの前に this-> を追加する必要があるのはなぜですか? 私は、clang 3.1 が同じ動作を示すことに気付いたので、c++ 標準にこの動作を必要とする何らかの要件があると思います。誰かがそれを正当化できますか?