Topic: typename" not useful for parsing


Author: mmann@ibm.net (Gerd Moellmann)
Date: 1996/06/05
Raw View
The keyword `typename' was introduced (if I interpret the intention
right) to make it easier to parse template declarations so that
parsing errors can be detected at the time the template declaration is
parsed instead of when a template is instantiated.

After having implemented a C++ front end, I propose to reconsider this
decision and remove the typename keyword from the standard because it
potentially breaks existing code.  My claim is that existing language
constructs plus two simple rules can replace it without giving up the
original intent.

The rules I propose are:

  1. When there is no ambiguity decl vs. expr, parse a template
     parameter depdendant name as a type in declarations, and as
     a non-type in using declarations and expressions.

  2. When there is an ambiguity decl vs. expr at a template parameter
     dependant name, parse the name as a non-type.


Rule 1:
-------

A template parameter dependant name cannot be a declarator id, except
in friend declarations.  In a context where only declarations are
permissable, rule 1 therefore results in what one would expect.

   template <class T> class X : public T::X {
       typedef T::X TX;
       T::X f (T::X &x);
       using T::X::f;
       friend T::Y T::g ();
   };

A declaration of the form

    friend const T::g ();

would be an error under rule 1 but it could be easily resolved with
existing language features by writing

    friend const int T::g ();

Using declarations would be a special case because we know that the
name that follows must be a function name.

Rule 2:
-------

   template <class T>
   T::X X::f (T::X &x) {
       T::X (2); // (1)
       auto T::X (2); // (2)
       const T::X x; // (3)
   }

Line (1) would be parsed as a function call of `T::X' under rule 2.  The
`auto trick' could be used to make it a declaration.  Line (3) would be
parsed as a declaration because there is no ambiguity at the place where
`T::X' appears.  There is also always the possibility to use a typedef

    typedef T::X TX;

which is itself not ambiguous to force an interpretation as a type.

--
Gerd Moellmann, Altenbergstr. 6, D-40235 Duesseldorf, Germany
Software Development & Consulting
Internet: mmann@ibm.net / CIS: 100025,3303 / Phone: +49 211 666 881


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]