[Bug c++/53573] template type dependent name resolution broken

keean@fry-it.com gcc-bugzilla@gcc.gnu.org
Tue Jun 5 11:26:00 GMT 2012


--- Comment #13 from Keean Schupke <keean@fry-it.com> 2012-06-05 11:25:40 UTC ---
(In reply to comment #9)

>From ISO14882 14.6 - Name resolution [temp.res]

-8- When looking for the declaration of a name used in a template definition,
the usual lookup rules (basic.lookup.unqual, basic.lookup.koenig) are used for
nondependent names. The lookup of names dependent on the template parameters is
postponed until the actual template argument is known (temp.dep). [Example:

#include <iostream>
using namespace std;

template<class T> class Set {
    T* p;
    int cnt;
    Set<T>(const Set<T>&);
    void printall()
        for (int i = 0; i<cnt; i++)
            cout << p[i] << '\n';
    //  ...
in the example, i is the local variable i declared in printall, cnt is the
member cnt declared in Set, and cout is the standard output stream declared in
iostream. However, not every declaration can be found this way; the resolution
of some names must be postponed until the actual template-arguments are known.
For example, even though the name operator<< is known within the definition of
printall() and a declaration of it can be found in <iostream>, the actual
declaration of operator<< needed to print p[i] cannot be known until it is
known what type T is (temp.dep). ]
-9- If a name does not depend on a template-parameter (as defined in temp.dep),
a declaration (or set of declarations) for that name shall be in scope at the
point where the name appears in the template definition; the name is bound to
the declaration (or declarations) found at that point and this binding is not
affected by declarations that are visible at the point of instantiation.

void f(char);

template<class T> void g(T t)
    f(1);                   //   f(char)
    f(T(1));                //  dependent
    f(t);                   //  dependent
    dd++;                   //  not dependent
                //  error: declaration for dd not found
void f(int);

double dd;
void h()
    g(2);                   //  will cause one call of  f(char)  followed
                //   by two calls of  f(int)
    g('a');                 //  will cause three calls of  f(char)

So it was like this in 1998, and it is the same in the latest working draft. 

--- end example]

> (In reply to comment #2)
> > The function called in the template definition is clearly dependent on the
> > template parameter 'T' and therefore itsname should be resolved at the point of
> > instantiation where 'g' is clearly defined and in scope (and is not local). The
> > error message says: "no declarations were found by argument-dependent lookup at
> > the point of instantiation" when 'g' should be found. 
> Built-in types have no associated namespaces so ADL can not find 'g' via ADL. A
> declaration must be visible at the point of definition.
> The example in comment 1 is ill-formed.
> Paragraph 10 doesn't apply because the call is clearly dependent.
> N.B. N3242 is not the C++ standard and neither is "C++ Special Edition" which
> is quite old now.

More information about the Gcc-bugs mailing list