Topic: template parsing ambiguity


Author: rickh@csn.net (Rick Hollinbeck)
Date: 1995/07/24
Raw View
Some template name scoping ambiguities do not seem clear from the ISO draft...

Given this code:

class a;
enum e { enumval1 = 1 };
template<class T> class A : public T {
 a x;
 void b() {
  enum e i = enumval1;
  }
 };

How should this be parsed? The draft standard is not clear (at least to me!)

The problem is that 'a' may be a nested type name of class template
argument T, e.g.

class b {
 typedef int a;
 enum e { enumval1 = 2 };
 };

A<b> Ab;   // Instantiation should interpret "a x;" as the nested class.
           // and enumval1 should be '2'.

Should it now be written:

template<class T> class A : public T {
 typename T::a x;
 void b() {
  enum T::e i = T::enumval1;  // Are these T:: qualifiers both required?
 }
 }

If written the first way, 'a' would early-bind to the global classname.
Similarly, enum e and enumval1 would bind to global scope.
Should a diagnostic be issued at the instantiation point due to the ambiguity?

Many existing compilers perform late token binding. So, the meaning of this
template may silently change when the standard is implemented.

If the forward declaration of class a; were missing in the first example,
is this a syntax error, or must the parser "guess" that 'a' must be a nested
typename of T and mark it for late binding?

The draft standard is unclear about this (unless I missed a section
(entirely possible!) Perhaps a section should be added clarifying
template classes derived from a template argument. Comments anyone?

- Rick Hollinbeck