Boost logo

Boost :

From: (nil)
Date: 2000-02-16 12:01:56


> > 5. I think you may need "typename" on the "const T value_" and "T&
> > variable_", and maybe the typedefs too. I've never been able to figure
out
> > what the requirements are. Does anyone know of a good (easily
> > understandable) reference for learning where typename is required?
>
> You definitely don't need typename in those cases. The only reference I
know
> of is the standard, and I'd love to find something easier to understand.
>
> In a nutshell, it's only dependent types that require typename. A template
> parameter itself is not a dependent type. It requires a template parameter
> on the left side of something with a "::" in it, for the most part.
>

A template parameter *is* a dependent type; however, it should not be used
with typename because it is an unqualified name.

I used to be confused on this subject as well, and my compiler (BCB4) didn't
help -- it never *requires* typename, and allows spurious typenames where
their usage should be illegal. I have written a small article, not yet
published (maybe never will be), on the proper (portable) usage of typename.

Just follow the following guidelines, which are derived from the Standard,
but hopefully easier to understand. :)

1) typename may never be used except in template declarations or definitions
[14.6/5]

2) typename may never be used to refer to a name of something other than a
type (for example, a static const value or a function) [14.6/4]

3) typename may never be used in unqualified names, where a name is
"qualified" if it contains "::" at its outermost level (meaning a "::" in a
template argument doesn't count, see below) [14.6/5]

4) typename is required for use with dependent names (it is optional for use
with independent names) [14.6/2] [14.6/3]

5) typename should be considered for use for names and any template
parameters within those names; this may require nested usage of typename's
(for example, "typedef X<typename T::type> type;" and "typedef typename
X<typename T::type>::type type;" are correct) [14.6/3]

6) typename may never be used in base class lists or member initializer
lists, unless used as part of a template parameter from rule (5) [14.6/5]

The most confusing part of these guidelines is that dependency (4) is
"inherited" from template parameters (i.e., if "T" is dependent, then "X<T>"
is dependent), but qualification (3) is not (i.e., "T::type" is qualified,
but "X<T::type>" is unqualified).

Summary of qualification/dependency rules (3 and 4):
  Name is Qualified and Dependent -> typename is required
  Name is Qualified and Independent -> typename is optional
  Name is Unqualified and Dependent -> typename is illegal
  Name is Unqualified and Independent -> typename is illegal

Best regards -

        -Steve

P.S. If you're checking the Standard references, note that there is an open
issue, #121, which clarifies [14.6/3].


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk