Topic: Template inheritance from type parameter?
Author: jlilley@empathy.com (John Lilley)
Date: 1996/12/09 Raw View
I'm working on a C++ parser, and have found a potential template syntax that
seem to violate the spirit of the draft standard, but I cannot find where it
is explicitly disallowed. Take the following code for example:
template <class T> class A : public T { I foo(); };
In order to determine if "I" is a type or an identifier, one must have the
base class definition, which in this case is the parameterized type "T".
If this type of code were allowed, then the template body could not be parsed
until the template was specialized, which seems to be a violation of the
intent that template bodies could, in theory, be parsed and stored in a syntax
tree rather than as raw tokens. This is fundamentally different then a
template class that inherits from another template class, as in:
template <class T> class B : public list<T> { ... };
Because in the latter case, the determination of which identifiers are types
is presumably not influenced by the parameterization of the base class.
Can someone enlighten me on this one?
john lilley
[ 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: fwai@armltd.co.uk (Francis Wai)
Date: 1996/12/10 Raw View
jlilley@empathy.com (John Lilley) writes:
>I'm working on a C++ parser, and have found a potential template syntax that
>seem to violate the spirit of the draft standard, but I cannot find where it
>is explicitly disallowed. Take the following code for example:
> template <class T> class A : public T { I foo(); };
>In order to determine if "I" is a type or an identifier, one must have the
>base class definition, which in this case is the parameterized type "T".
A name in a template declaration is not assumed to name a type unless it is
in the enclosing scopes and the base class is one such. In order that 'I'
is intended to be a type name from the base class 'T', one would have to say,
"typename T::I foo();".
--Francis
[ 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: Jason Merrill <jason@cygnus.com>
Date: 1996/12/10 Raw View
>>>>> John Lilley <jlilley@empathy.com> writes:
> I'm working on a C++ parser, and have found a potential template syntax that
> seem to violate the spirit of the draft standard, but I cannot find where it
> is explicitly disallowed. Take the following code for example:
> template <class T> class A : public T { I foo(); };
> In order to determine if "I" is a type or an identifier, one must have the
> base class definition, which in this case is the parameterized type "T".
Nope; the compiler can assume that I is not a type. You must write
template <class T> class A : public T { typename T::I foo(); };
14.6 Name resolution [temp.res]
1 A name used in a template is assumed not to name a type unless the
applicable name lookup finds a type name or the name is qualified by
the keyword typename.
Jason
---
[ 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: esap@cs.tut.fi (Pulkkinen Esa)
Date: 1996/12/10 Raw View
John Lilley <jlilley@empathy.com> wrote:
>I'm working on a C++ parser, and have found a potential template syntax that
>seem to violate the spirit of the draft standard, but I cannot find where it
>is explicitly disallowed. Take the following code for example:
>
> template <class T> class A : public T { I foo(); };
>
>In order to determine if "I" is a type or an identifier, one must have the
>base class definition, which in this case is the parameterized type "T".
In the above case, 'I' must have a declaration before the template is
introduced. To make 'I' a type that depends on T you'll have to write:
typename T::I foo(); // typename keyword ensures this is a type,
// explicit qualification ensures it's dependent
// on the template argument.
And if you wanted to refer to a static member from T,
you'd have to write: (for example)
static const int y = T::x; // note explicit qualification.
The relevant sections of the DWP (at least in April'95 DWP) are:
[temp.res]/1..7 and [temp.dep].
I think D&E also addresses this point.
>If this type of code were allowed, then the template body could not be parsed
>until the template was specialized, which seems to be a violation of the
===========
I think you mean instantiated?
>intent that template bodies could, in theory, be parsed and stored in
>a syntax tree rather than as raw tokens.
With the above rule and the sections cited, it should be possible to
parse the declarations at template definition.
--
Esa Pulkkinen | C++ programmers do it virtually
E-Mail: esap@cs.tut.fi | everywhere with class, resulting
WWW : http://www.cs.tut.fi/~esap/ | in multiple inheritance.
---
[ 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
]