Topic: typename versus typedef (was typedef X;)


Author: martelli@cadlab.cadlab.it (Alex Martelli)
Date: 1995/09/25
Raw View
ark@research.att.com (Andrew Koenig) writes:

>In article <MATT.95Sep23125440@Austern>
>martelli@cadlab.cadlab.it (Alex Martelli) writes:

>> avoiding the template author the chore of thinking up a dummy type
>> name in this situation...?  Or am I missing something here...?

>You're missing something.

> template<class T> class X {
>  int a;
>  void f() {
>   T::Pointer (a);  // *
>  }
> };

>If T::Pointer is the name of a type, the line with the comment
>declares a local variable inside function f that is of type T::Pointer.
>If, on the other hand, T::Pointer is the name of a function, then
>the line with the comment calls that function passing it the
>member names `a' of class X.

So far, so clear.

>This shows that in general it is necessary for the compiler to know
>whether each identifier is the name of a type.  In the case of members
>of template parameters, there's no easy way to know that in advance,
>so the "typename" keyword provides a way to say.

And if typename is absent, then T::Pointer will be taken for not
a typename, right?

So, what would be wrong with the idiom I suggested could have been
used instead -- if T::Pointer is used as in your example, take it
as not a typename; if you need to use a type name qualified by a
template parameter class, you would then have to write:

 void f() {
     typedef T::Pointer anyoldname;
     anyoldname a;
 }

This _is_ unambiguous, isn't it?  T::Pointer _must_ be a type name
in this usage -- unless I'm missing something here, which I think
you still have not shown.

Is typename just syntactic sugar for something that could have been
obtained with a not-particularly-obscure idiom for this situation?
That's what I was asking when I asked "what happened to minimalism".
I guess there must be some reason to make the typedef-idiom substitute
for "typename" not workable or not desirable, but, what IS it?


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: martelli@cadlab.cadlab.it (Alex Martelli)
Date: 1995/09/23
Raw View
craig@mccabe.com (Craig P. Forbes) writes:
 ...
>My understanding is that the typename keyword may only be used in templates
>to identify subtypes of the template parameters, which otherwise can not be
>identified as a type.
 ...
>template <T>
>class foo {

>  typename T::i;
>  int var;

>  void fn() {
>    T::i *var;  //without the typename keyword this could either be a declaration

<blink>?  What happened to minimalism...?  Couldn't this be just as
well have been coded...:

 typedef T::i dummynameoftype; // no syntax ambiguity here
 ...
 dummynameoftype *var; // no ambiguity here either

Was a whole new keyword really introduced just for the purpose of
avoiding the template author the chore of thinking up a dummy type
name in this situation...?  Or am I missing something here...?


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: ark@research.att.com (Andrew Koenig)
Date: 1995/09/25
Raw View
In article <MATT.95Sep23125440@Austern>
martelli@cadlab.cadlab.it (Alex Martelli) writes:

> avoiding the template author the chore of thinking up a dummy type
> name in this situation...?  Or am I missing something here...?

You're missing something.

 template<class T> class X {
  int a;
  void f() {
   T::Pointer (a);  // *
  }
 };

If T::Pointer is the name of a type, the line with the comment
declares a local variable inside function f that is of type T::Pointer.
If, on the other hand, T::Pointer is the name of a function, then
the line with the comment calls that function passing it the
member names `a' of class X.

This shows that in general it is necessary for the compiler to know
whether each identifier is the name of a type.  In the case of members
of template parameters, there's no easy way to know that in advance,
so the "typename" keyword provides a way to say.
--
    --Andrew Koenig
      ark@research.att.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]