Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name lookups in C++ templates

I have some C++ code that is no longer compiling without the -fpermissive option. It's propriety code that I can't share, but I've think I've been able to extract a simple test case that demonstrates the problem. Here is the output from 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 

So here is the code the generates the problem:

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); } 

I am not trying to find out how to fix the code to make it compile again. That's simply a matter of changing next(item) to this->next(item) I'm trying to better understand why this change is necessary. I found an explanation on this page: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html While that explanation was useful, I still have some questions. Shouldn't the fact that my function takes a T* (pointer to type T) make it dependent on the template argument. In my own wording, shouldn't the compiler (gcc 4.7) be able to figure out that the next() function is in the base class List? Why is it necessary to prepend this-> in front of every such call? I've notice that clang 3.1 exhibits the same behavior, so I assume that there is some requirement in the c++ standard that requires this behavior. Could anyone provide a justification for it?

like image 721
Avatar33 Avatar asked May 17 '12 15:05

Avatar33


2 Answers

The problem is that templates are processed in two passes (according to the standard, VS does otherwise). In the first pass, before the type substitution, everything that does not depend on the template arguments is looked up and checked. Dependent names are then left to resolve in the second pass, once the type has been substituted.

Now, in the first pass there is nothing that indicates that next is dependent on template arguments, and thus it needs to resolve before type substitution. Now, because the base type is templated on the template argument of your current template, the compiler cannot look into it (it might be specialized for some types, and without knowing what type T we are instantiating the template with, we cannot know which specialization to use, i.e. the base depends on T and we are checking before knowing T).

The trick of adding this-> turns next into a dependent name, and that in turn means that lookup is delayed until the second pass, where T is known, and because T is known, List<T> is also known and can be looked up into.


EDIT: One important detail missing in the wording of the answer above is that second phase lookup (after type substitution) will only add functions found during argument dependent lookup. That is, if next was a free function in a namespace associated with T it would be found, but it is a member on the base, which is not visible for ADL on T.

like image 130
David Rodríguez - dribeas Avatar answered Sep 28 '22 04:09

David Rodríguez - dribeas


You need to write this-> as:

this->next(item); 

Here this-> part is required because next() is an inherited member from template base, and if you read the error message carefully, it is suggested there itself:

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

Read this article which has explained two-phase name lookup in C++:

  • The Dreaded Two-Phase Name Lookup
like image 27
Nawaz Avatar answered Sep 28 '22 03:09

Nawaz