lundi 2 mars 2015

Why is a member function in a template base class not found? [duplicate]



This question already has an answer here:




Here is a minimal code snippet.



template<class T> class Base
{
public:
void base_fn(T* arg) {/* do nothing */}
};

template<class T> class Derived : public Base<T>
{
public:
void derived_fn() {
base_fn(nullptr); // error here
}
};


This is the complete contents of test.cpp, and I am attempting to compile it with:



gcc -std=c++14 test.cpp


The output of gcc --version is:



gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


The output produced by GCC is:



test.cpp: In member function ‘void Derived<T>::derived_fn()’:
test.cpp:11:18: error: there are no arguments to ‘base_fn’ that depend on a template parameter, so a declaration of ‘base_fn’ must be available [-fpermissive]
base_fn(nullptr);
^
test.cpp:11:18: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)


If I change line 11 to this, it compiles successfully:



base_fn(static_cast<T*>(nullptr));


It also compiles if I change the definition of derived_fn to:



void derived_fn(T* arg) {
base_fn(arg);
}


Why does the original code not compile successfully?




Aucun commentaire:

Enregistrer un commentaire