Topic: C++ templates standard


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Wed, 14 Sep 1994 08:37:49 GMT
Raw View
In article <Cw06vx.1yu@hpgndlab.grenoble.hp.com> jr@grenoble.hp.com (Jean-Rene Bouvier) writes:
>In article <JASON.94Sep7141307@deneb.cygnus.com>, jason@cygnus.com (Jason Merrill) writes:
>> >>>>> Jean-Rene Bouvier <jr@grenoble.hp.com> writes:
>>
>> > I've tried several compilers for the following piece of code, but none
>> > accepted it.
>> > Does anyone know whether my code is legal or not ?
>>
>> It isn't.  Class templates cannot be nested.
>>
>> Jason
>
>Is there any reason - besides compilers complexity - to forbid nested
>templates.

Not that I can think of.  The only excuse for this particular bit of non-
orthogonality in the language that I can even imagine is that once upon a
time, templates were implemented via macro-processing... and macro processors
tend not to understand such things as nested scopes.

Given that those times have now passed, I also would like to know why
templates cannot be nested.

>I can't see why there would be an issue with them...

Me neither.

>has that question been debated at ANSI ?

Not as far as I know.  Up until quite recently, the standardization
committee has been too busy shoveling new stuff into the language
wholesale to spend very much time looking back and wondering about
the quirks of the *existing* language features.  (I should say however
that there are many valiant souls... not the least of which is John
Spicer of EDG, who has done one hell of a lot of work on templates...
who have been working hard for a long long time to put putty in all
those annoying little holes.)

>BTW: none of my compilers told me I was using an illegal construct; indeed one
>of them even spat a 'sorry, not implemented...

Well... so it's not implemented. :-)

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -




Author: jr@grenoble.hp.com (Jean-Rene Bouvier)
Date: Mon, 12 Sep 1994 06:19:08 GMT
Raw View
In article <JASON.94Sep7141307@deneb.cygnus.com>, jason@cygnus.com (Jason Merrill) writes:
> >>>>> Jean-Rene Bouvier <jr@grenoble.hp.com> writes:
>
> > I've tried several compilers for the following piece of code, but none
> > accepted it.
> > Does anyone know whether my code is legal or not ?
>
> It isn't.  Class templates cannot be nested.
>
> Jason

Is there any reason - besides compilers complexity - to forbid nested
templates. I can't see why there would be an issue with them, has that
question been debated at ANSI ?

BTW: none of my compilers told me I was using an illegal construct; indeed one
of them even spat a 'sorry, not implemented (I know, no compiler holds the
truth...).

- jr.




Author: jr@grenoble.hp.com (Jean-Rene Bouvier)
Date: Mon, 12 Sep 1994 06:26:22 GMT
Raw View
In article <34l5ic$702@sndsu1.sinet.slb.com>, allen@chesapeake.rps.slb.com (Marc L. Allen) writes:
> Jean-Rene Bouvier (jr@grenoble.hp.com) wrote:
> > Does anyone know whether my code is legal or not ?
>
> > class List { // abstract list
> > public:
> >  template<class Elem>
> >  class Of : public List {
> >  public:
> >   // implementation details
> >  };
> > };
>
> [ rest deleted ]
>
> It looks to me as if you've got a subclass (Of) defined based on a List
> class.  This is a recursive definition.
>
> Since Of is a subclass of List, it would inherit it's own Of subclass, which
> is based on a List class, so IT would have it's own...
>
> Marc
>
>

I'm not sure there's any recursivity involved here (class Of<> is not contained
in class List, it is just in its name scope). Yet, there might be a problem
due to the fact that class List isn't completely defined at the time class
Of<> inherits from it, but with a decent compiler supporting nested class
forward declarations, this shouldn't be a problem, the example could be
rewritten as:

class List { // abstract list
public:
 template<class Elem> class Of;
};

template<class Elem>
class List::Of : public List {
public:
 // implementation details
};

Or am I wrong ?

- jr




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 13 Sep 1994 21:49:38 GMT
Raw View
In article 1yu@hpgndlab.grenoble.hp.com, jr@grenoble.hp.com (Jean-Rene Bouvier) writes:
>In article <JASON.94Sep7141307@deneb.cygnus.com>, jason@cygnus.com (Jason Merrill) writes:
>> >>>>> Jean-Rene Bouvier <jr@grenoble.hp.com> writes:
>>
>> > I've tried several compilers for the following piece of code, but none
>> > accepted it.
>> > Does anyone know whether my code is legal or not ?
>>
>> It isn't.  Class templates cannot be nested.
>>
>> Jason
>
>Is there any reason - besides compilers complexity - to forbid nested
>templates. I can't see why there would be an issue with them, has that
>question been debated at ANSI ?

This is explained in detail in "The Design and Evolution of C++".

When the template feature was being designed, no one knew what all the
consequences would be of nested templates, and there were no actual
implementations of templates at the time the ARM was written. Rather than
allow something that might later have to be removed from the language,
Stroustrup made the rule that templates may appear only at file scope.

Later experience has showed that there are indeed some problems, particularly
with allowing template virtual functions, but that for most cases the rule
about file scope can be relaxed. The C++ Committee has approved a relaxed
rule. Few, if any, compilers allow nested templates yet because the feature
is too new.

---
Steve Clamage, stephen.clamage@eng.sun.com






Author: allen@chesapeake.rps.slb.com (Marc L. Allen)
Date: 7 Sep 1994 19:50:36 GMT
Raw View
Jean-Rene Bouvier (jr@grenoble.hp.com) wrote:
> Does anyone know whether my code is legal or not ?

> class List { // abstract list
> public:
>  template<class Elem>
>  class Of : public List {
>  public:
>   // implementation details
>  };
> };

[ rest deleted ]

It looks to me as if you've got a subclass (Of) defined based on a List
class.  This is a recursive definition.

Since Of is a subclass of List, it would inherit it's own Of subclass, which
is based on a List class, so IT would have it's own...

Marc






Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 7 Sep 1994 21:13:07 GMT
Raw View
>>>>> Jean-Rene Bouvier <jr@grenoble.hp.com> writes:

> I've tried several compilers for the following piece of code, but none
> accepted it.
> Does anyone know whether my code is legal or not ?

It isn't.  Class templates cannot be nested.

Jason




Author: jr@grenoble.hp.com (Jean-Rene Bouvier)
Date: Tue, 6 Sep 1994 11:54:08 GMT
Raw View
I've tried several compilers for the following piece of code, but none
accepted it.
Does anyone know whether my code is legal or not ?
The code defines an abstract base class "List" that is used as a base class
for template class List::Of<>. It also define an interface for the list in
the form of another base class List::Client that Elements need to derive from
in order to be insertible in the list.
Here is the skeleton for the code
(I've removed all methods from class definitions, just to show that
what doesn't work is the nested template class):

class List { // abstract list
public:
 // class List pure virtual methods would go here.
 class Client { // List clients must derive from this class
 public:
  // methods go here
 private:
  Client* next;
  Client* prev;
 };
 template<class Elem>
 class Of : public List {
 public:
  // implementation details
 };
};

// example of use:
class Object : public List::Client {
public:
 // methods go here
};

main() {
 List::Of<Object> list;
 Object *object = new Object;
 object->insertOn(list);  // assuming insertOn() is part of
     // List::Client public i/f.
 return 0;
}

- jr.