Topic: A typename question


Author: s3824888@techftp.technion.ac.il (Louidor Erez )
Date: 1998/10/04
Raw View
In the following code:

template<class T>
struct Vector{
  typedef T* iterator;
};

template<class T>
void f(){
  /* typename? */ Vector<T>::iterator i;
}

Do I need the typename keyword in the declaration of i?

I've been told that since there might be a specialization of
Vector the compiler cannot simply deduce from the typedef
in Vector that iterator is a type, and therefore the typename
keyword is required. But Stroustrup had this exact example
in his 'The C++ Programming Language / 3rd Edition' on pages
856-858. I've tried looking in the errata for this book but
found no mentions to this example.

I would very much appreaciate it, if someone could not only
answer my question but also support it with a reference/quote
to/from the standard.


 Thanks,

 Erez.








[ 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              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/10/05
Raw View
Louidor Erez <s3824888@techftp.technion.ac.il> writes:

> template<class T>
> struct Vector{
>   typedef T* iterator;
> };

> template<class T>
> void f(){
>   /* typename? */ Vector<T>::iterator i;
> }

> Do I need the typename keyword in the declaration of i?

didn't believe me in the egcs mailing list, eh? :-)

The C++ Standard says [temp.dep]:

3 A qualified-name that refers to a type and that depends on a template-
  parameter  (_temp.dep_)  shall  be prefixed by the keyword typename to
  indicate that the qualified-name denotes a  type,  forming  an  elabo-
  rated-type-specifier (_dcl.type.elab_).

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ 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              ]





Author: Matt Austern <austern@sgi.com>
Date: 1998/10/05
Raw View
s3824888@techftp.technion.ac.il (Louidor Erez ) writes:

> In the following code:
>
> template<class T>
> struct Vector{
>   typedef T* iterator;
> };
>
> template<class T>
> void f(){
>   /* typename? */ Vector<T>::iterator i;
> }
>
> Do I need the typename keyword in the declaration of i?

Yes.  From 14.6, paragraph 3: "A qualified-name that refers to a type
and that depends on a template-parameter (14.6.2) shall be prefixed by
the keyword typename to indicate that the qualified-name denotes a
type, forming an elaborated-type-specifier (7.1.5.3)."

In this example, Vector<T>::iterator is a qualified-name (it's
qualified by "Vector<T>::").  It depends on the template parameter T,
because it involves a template-id whose template argument is T.
(14.6.2.1, paragraph 1, clause 6.)
---
[ 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              ]