Topic: Typename -- just a little behind


Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1998/02/06
Raw View
Tim Ottinger  <ottinger@oma.com> wrote:
>> I hate to run out and buy a book just to learn this (seemingly small)
>> thing. Maybe I need to go browse C++PL(3) and sit in the coffee bar?

[Note: C++PL(3) = "The C++ Programming Language", 3rd edition,
by Bjarne Stroustrup.]

Nathan Myers wrote:
> Sit in the coffee bar as long as you like, but buy the book anyway.
> You will certainly need it if you hope to continue giving your
> clients full value.  You can justify the expense by learning a
> great variety of (small) things.  (Be sure to get the 5th (or later)
> printing.)

Why the 5th or later printing?  (I've got the 1st printing, Jun'97).
I assume it's because of the errata list; this is available on
Bjarne's web page (http://www.research.att.com/~bs/3rd_errata.html).

-- David R. Tribble, david.tribble@noSPAM.central.beasys.com --
---
[ 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/std-c++/faq.html                  ]





Author: Phlip <tegan@deltanet.com>
Date: 1998/02/05
Raw View
Tim Ottinger escribi=F3:

> Thanks for this, but I didn't see how in the given example:
>
>          template<class T>
>          class Wrapper
>          {
>                  // ...
>                  typename T::X data;     // X is a type member
>                                          // of the parameter T
>          };
>
> The "typename" was necessary. What else could T::X be besides
> a type from the namespace T?...

    int data (T::X);

Is 'data' a (poorly named) function that takes an argument of type 'T::X'=
? or an
integer assigned with the value 'T::X'?

We (maybe) don't like this 'data' to change its intrinsic meaning between=
 two
template instances.

> Maybe I need to go browse C++PL(3) and sit in the coffee bar?

It's the only way to fly!

  --  Phlip
=3D=3D=3D=3D=3D=3D=3D http://users.deltanet.com/~tegan/home.html =3D=3D=3D=
=3D=3D=3D=3D
  --  Spacetime is money  --
---
[ 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                             ]





Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/02/05
Raw View
Tim Ottinger  <ottinger@oma.com> wrote:
>I hate to run out and buy a book just to learn this (seemingly small)
>thing. Maybe I need to go browse C++PL(3) and sit in the coffee bar?

Sit in the coffee bar as long as you like, but buy the book anyway.
You will certainly need it if you hope to continue giving your
clients full value.  You can justify the expense by learning a
great variety of (small) things.  (Be sure to get the 5th (or later)
printing.)

Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/
---
[ 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                             ]





Author: David Vandevoorde <daveed@cup.hp.com>
Date: 1998/02/06
Raw View
Tim Ottinger wrote:
>=20
> Where do I catch up on the new typename keyword, meaning the
> intended usage, explanation of need, and maybe an example usage
> or two?

Allow me to plug a book I am writing for Addison-Wesley ;-)
It's a companion book for Bjarne Stroustrup's 3rd edition of
"The C++ Programming Language" (which you should definitely=20
get!) that discusses solutions to select exercises proposed
by Stroustrup. Don't run to your local bookshop just yet
though... publication is still several months away.

Here is a short excerpt from my draft. I removed cross-refs=20
and compromised on typesetting.

[... excerpt start ...]
The keyword typename is used to indicate that the name=20
following the keyword does in fact denote the name of=20
a type. However, you cannot just liberally sprinkle your
code with typename wherever you name a type. More precisely,
you _must_ use the keyword typename in front of a name that:

(1) is qualified: i.e., it contains a scope-resolution
    operator =91::=92; and
(2) appears in a template; and
(3) has a component left of a scope resolution operator
    that depends on a template _parameter_ (not a template
    _argument_); and
(4) denotes a type; and
(5) is not used in a list of base-classes or as an item to
    be initialized by a constructor initializer list.

Furthermore, you are _only allowed_ to use the keyword in this
sense if all of the above apply, except perhaps the third point.
To illustrate this rather subtle rule, consider this erroneous
code fragment:

   template<typename1 T>
   struct S: typename2 X<T>::Base {
      S(): typename3 X<T>::Base(typename4 X<T>::Base(0)) {}
      typename5 X<T> f() {
         typename6 X<T>::C *p; // Declaration of pointer p
         X<T>::D *q; // Multiplication!
      }
      typename7 X<int>::C *s_;
   };

   struct U {
      typename8 X<int>::C *pc_;
   };

Consider each occurrence of typename in this example. They are=20
numbered with subscripts for easy reference. The first,
typename1, indicates a template argument: the rules above do
not apply to this first usage. The second and third typenames
are disallowed by the fifth item in the rules above: base-class
names in these two contexts cannot be preceded by typename.=20
However, typename4 is required: here, the name of the base-class=20
is not used to denote what is being initialized or derived from.
Instead, the name is part of an expression to construct a
temporary X<T>::Base from its argument 0 (a sort of conversion,
if you will). The fifth typename is prohibited because the name
that follows it, X<T>, is not a so-called qualified name. The
sixth occurrence is required if this statement is to declare a
pointer. The next line omits the typename keyword and is
therefore interpreted by the compiler as a multiplication. This
illustrates the fundamental reason why typename was introduced
into the language: it allows compilers to better "understand"=20
the intent of your templates, thereby producing earlier and more
accurate diagnostics when the code contains syntax errors. The
seventh typename is optional because it satisfies all the rules
above, except the third. Finally, typename8 is prohibited=20
because it is not used inside a template.
[... excerpt end ...]

That should help you out,

        Daveed


[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/02/04
Raw View
Tim Ottinger  <ottinger@oma.com> wrote:
>Where do I catch up on the new typename keyword, meaning the
>intended usage, explanation of need, and maybe an example usage
>or two?

Sean Corfield's page is a good place to start:

  http://www.ocsltd.com/c++/

Look under "templates".  (Mind you don't bump your head on the frames.)

Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/


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





Author: Tim Ottinger <ottinger@oma.com>
Date: 1998/02/04
Raw View
Nathan Myers wrote:

> Sean Corfield's page is a good place to start:
>   http://www.ocsltd.com/c++/
>

Thanks for this, but I didn't see how in the given example:


         template<class T>
         class Wrapper
         {
                 // ...
                 typename T::X data;     // X is a type member
                                         // of the parameter T
         };

The "typename" was necessary. What else could T::X be besides
a type from the namespace T? I assume that there are more complex
cases where "template" is not appropriate but "typename" is needed?
I hate to run out and buy a book just to learn this (seemingly small)
thing. Maybe I need to go browse C++PL(3) and sit in the coffee
bar?

Tim
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Tim Ottinger <ottinger@oma.com>
Date: 1998/02/02
Raw View
Where do I catch up on the new typename keyword, meaning the
intended usage, explanation of need, and maybe an example usage
or two?

I blinked, and when I looked again, there were typenames all
over the place. (Am I the only one?).

Tim
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: schuerig@acm.org (Michael Schuerig)
Date: 1998/02/03
Raw View
Tim Ottinger <ottinger@oma.com> wrote:

> Where do I catch up on the new typename keyword, meaning the
> intended usage, explanation of need, and maybe an example usage
> or two?

C++PL3, C.13.5

Or, to quote Stroustrup: "If we want to state that something should be
treated as a type, we can do so using the typename keyword." (p. 858)

Lacking psychic compilers typename is just a way to tell them that the
thing they're trying to parse denotes a type. This is useful in template
declarations when refering to "something" that's qualified with the name
of a template argument -- how do you (the compiler) know what that
something is? typename makes it clear that it's a type.

Michael

---
Michael Schuerig
mailto:schuerig@acm.org
http://www.uni-bonn.de/~uzs90z/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]