Topic: Name Resolution within a Template


Author: Marc Girod <girod@stybba.ntc.nokia.com>
Date: 1998/08/21
Raw View
Hello!

I just read "Inside the C++ Object Model" by Stanley Lippman, and
among others I have a doubt about a paragraph pp 248-249.

He gives an example, which I keyed in as follows (below).

His commentary goes:

     [...] which instance of foo() is invoked [?...] the instance
     chosen is the nonintuitive one.
     [...] The program site of the resolution of a nonmember name within
     a template is determined by whether the use of the name is dependent
     on the parameter types used to instantiate the template. If the use
     is not dependent, then the scope of the template declaration
     determines the resolution of the name. If the use is dependent, the
     the scope of the instantiation determines the resolution of the
     name.

Well, my compiler (HP aCC, 1.12) doesn't behave as predicted.

The code:
--------------------------------
#include <iostream.h>

double foo(double);

template <class T> class ScopeRules {
  public:
    void inv() { m_ = foo(v_); }
    T t_dep() { return foo(m_); }
  private:
    int v_;
    T m_;
};

int foo(int);

int main() {
    ScopeRules<int> sr0;
    cout << "sr0.inv():\n\t";
    sr0.inv();
    cout << "sr0.t_dep():\n\t";
    sr0.t_dep();
    return 0;
}

double foo(double) {
    cout << "double foo(double)" << endl;
    return 0.0;
}

int foo(int) {
    cout << "int foo(int)" << endl;
    return 0;
}
--------------------------------

My transcript:

$ aCC -o foo foo.C
$ foo
sr0.inv():
 int foo(int)
sr0.t_dep():
 int foo(int)

According to Lippman, in the first case, the "double" version should
have been selected.

Is this assumption wrong? obsolete? did I make an error in reproducing
his example (e.g. by inlining the foos?)? or should I file a bug
report?

--
Marc Girod                Valimo 1/2         Voice:  +358-9-511 63331
Nokia Telecommunications  P.O. Box 315       Mobile: +358-40-569 7954
NWS/NMS/NMS for Data      00045 NOKIA Group  Fax:    +358-9-511 63310
                          Finland            marc.girod@ntc.nokia.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]