Article View: comp.std.c++
Article #3614Re: typename?
From: Matt Austern
Date: Fri, 08 Mar 1996 03:23
Date: Fri, 08 Mar 1996 03:23
50 lines
2770 bytes
2770 bytes
In article <m0tupLs-000CajC@sqarc.sq.com> willer@carolian.com (Steve Willer)
writes:
> I saw in the brochure for Borland C++ 5.0 that it will include support for
> numerous new C++ features, including the "typename" keyword. Could anyone
> enlighten me as to what that is? I couldn't find it in the April WP.
>
> This isn't equivalent to the "typeof" keyword that I want so much, is it?
No. The typename keyword is a consequence of the new rules for name
resolution in templates. Brief summary: there's a new disambiguation
rule saying that a name used in a template is assumed not to be a type
unless it has been explicitly declared to be a type.
Disambiguation rules like this are good, since they make it possible
to check template syntax even before the template has been
instantiated. This one, though, presents a problem. Suppose you want
to write code like this.
template<class Container>
void f(Container X)
{
Container::value_type tmp = *(X.begin());
// do something with tmp.
}
This is a syntax error, since Container::value_type is assumed not to
be the name of a type. (And I hope everyone agrees that this isn't a
contrived example. The STL source is full of examples like this.)
The typename keyword was introduced to solve this problem; it says
that the name following it refers to a type. The correct way to write
this code snippet, then, is as follows.
template<class Container>
void f(Container X)
{
typename Container::value_type tmp = *(X.begin());
// do something with tmp.
}
This is described in section 14.2 [temp.res] of the WP.
--
Matt Austern
SGI: MTI Compilers Group
austern@isolde.mti.sgi.com
---
[ 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.
]
Technical Details
Message-ID:
<AUSTERN.96Mar7183544@isolde.mti.sgi.com>Path:
arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-backReferences:
<m0tupLs-000CajC@sqarc.sq.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-back From: austern@isolde.mti.sgi.com (Matt Austern) Newsgroups: comp.std.c++ Subject: Re: typename? Date: 08 Mar 96 03:23:54 GMT Organization: SGI Approved: fjh@cs.mu.oz.au Message-ID: <AUSTERN.96Mar7183544@isolde.mti.sgi.com> References: <m0tupLs-000CajC@sqarc.sq.com> Reply-To: austern@cardboard.mti.sgi.com NNTP-Posting-Host: munta.cs.mu.oz.au X-Original-Date: 08 Mar 1996 02:35:44 GMT In-Reply-To: willer@carolian.com's message of 08 Mar 96 02:10:08 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMT+oWuEDnX0m9pzZAQFlWgF/RqKnJx+GiRm/anS+TbETJir01s8FA2/9 uFjeE77GIgp3iRGgs3Nj1X1b773bmeUg =WrRF Originator: fjh@munta.cs.mu.OZ.AU