C++ Name Lookup in template

Here is an example in The C++ Programming Language written by Bjarne Stroustrup, on page 53:


#include 
#include 
#include 
#include 

using namespace std;

struct Entry{
    string name;
    int number;
};

template  class Vec : public vector {
public:
    Vec() : vector() {}
    Vec(int s) : vector(s) {}
    T& operator[](int i) { return at(i); }
    const T& operator[](int i) const {return at(i);}
};

If you complie this code, you will get the following error information:

HelloWorld.C: In member function ‘T& Vec::operator[](int)’:
HelloWorld.C:17: warning: there are no arguments to ‘at’ that depend on a template parameter, so a declaration of ‘at’ must be available
HelloWorld.C: In member function ‘const T& Vec
::operator[](int) const’:
HelloWorld.C:18: warning: there are no arguments to ‘at’ that depend on a template parameter, so a declaration of ‘at’ must be available

To make this code valid, either use this->at(i), or vector::at(i).

Note that some compilers (including G++ versions prior to 3.4) get these examples wrong and accept above code without an error. from http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

Reference
1. Name lookup - Using the GNU Compiler Collection (GCC). April 24, 2008

   Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.