Topic: Composition Techniques from D&E
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/29 Raw View
Siemel Naran wrote:
>
> [AllanW]
> >Wouldn't this be a better way of defining lli2?
> >
> > typedef List<List<int> > ListInt2;
> > ListInt2 lli2;
>
> Yes, this is better. But this even better
> template <class T> typedef List<List<T>> List2;
> On a recent post to comp.lang.c++.moderated, I demonstrated that
> template typedefs are allowed by the BNF grammar. So a conforming
> compiler should accept it, yet none do!
Of course none allow it because it isn't allowed.
But I really hope to see it in C++2008 (or whatever
the next std is).
Until it is allowed, we have to use typedefs in a template
class (the_class<T>::the_typedef instead of the_typedef<T>).
--
Valentin Bonnard
---
[ 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: comeau@panix.com (Greg Comeau)
Date: 1999/05/27 Raw View
In article <slrn7ke755.hi8.sbnaran@dirac.ceg.uiuc.edu> sbnaran@KILL.uiuc.edu writes:
>On a recent post to comp.lang.c++.moderated, I demonstrated that
>template typedefs are allowed by the BNF grammar.
I'm pretty sure you are correct.
>So a conforming compiler should accept it
No, language != BNF grammar alone.
>yet none do!
Nor should they.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- New Release! We now do Windows too.
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.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_mti/std-c++/faq.html ]
Author: AllanW <allan_w@my-dejanews.com>
Date: 1999/05/22 Raw View
D&E section 15.8 begins:
<quote>
Templates support several safe and powerful composition
techniques. For example, templates can be applied
recursively:
template<class T>class List { ?* ... */ };
List<int> li;
List<List<int> > lli;
List<List<List<int> > > llli;
If specific "composed types" are needed, they can be
specifically defined using derivation:
template<class T>class List2 : public List<List<T> > {};
template<class T>class List3 : public List2<List<T> > {};
List2<int> lli2;
List3<int> llli3;
</quote>
Stroustrup says that this is an "unusual use of derivation because
no members are added." Then he goes on to tell us about a problem
that arises: variables of these composed types can be used like
their explicitly defined types, but not the other way around:
<quote>
lli = lli2; // ok
lli2 = lli; // error
</quote>
because public derivation defines a subtype relationship.
Wouldn't this be a better way of defining lli2?
typedef List<List<int> > ListInt2;
typedef List<List<List<int> > > ListInt3;
ListInt2 lli2;
ListInt3 llli3;
The difference here is that the typedef is specific to int;
we want to use a template which implies multiple levels. But
here's yet another version which gets around this by combining
a template with a typedef:
template<class T>class ListN {
ListN(); // Never instanciate!
public:
template List<T> List1;
template List<List<T> > List2;
template List<List<List<T> > > List3;
template List<List<List<List<T> > > > List4;
};
ListN<int>::List2 lli2;
ListN<int>::List3 llli3;
I haven't tried this, but shouldn't it work just as well, and
without the disadvantages mentioned in D&E?
----
Allan_W@my-dejanews.com is a "Spam Magnet" -- never read.
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
---
[ 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: sbnaran@dirac.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/22 Raw View
[Stroustrup]
> template<class T>class List { ?* ... */ };
>
> List<List<int> > lli;
>
> template<class T>class List2 : public List<List<T> > {};
> List2<int> lli2;
>
> lli = lli2; // ok
> lli2 = lli; // error
[AllanW]
>Wouldn't this be a better way of defining lli2?
>
> typedef List<List<int> > ListInt2;
> ListInt2 lli2;
Yes, this is better. But this even better
template <class T> typedef List<List<T>> List2;
On a recent post to comp.lang.c++.moderated, I demonstrated that
template typedefs are allowed by the BNF grammar. So a conforming
compiler should accept it, yet none do! Here's a workaround
template <class T> struct List2 { typedef List<List<T>> type; };
> template<class T>class ListN {
> ListN(); // Never instanciate!
> public:
> template List<T> List1;
> template List<List<T> > List2;
> template List<List<List<T> > > List3;
> template List<List<List<List<T> > > > List4;
> };
This is pretty much the same as the above workaround.
But you forgot List5!!
So what we need are templated namespaces. But this would make
namespace qualification more confusing, so we can't have it. Eg,
namespace myspace {
template <class T> namespace yourspace {
template <class T> class X { };
}
}
And to use
int main() { myspace::template yourspace<int>::template X<int>(); }
As we can't have template namespaces, we just need to write
int main() { myspace::yourspace::X<int>(); }
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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: James Kuyper <kuyper@wizard.net>
Date: 1999/05/23 Raw View
Siemel Naran wrote:
....
> Yes, this is better. But this even better
> template <class T> typedef List<List<T>> List2;
> On a recent post to comp.lang.c++.moderated, I demonstrated that
> template typedefs are allowed by the BNF grammar. So a conforming
The grammar cannot be used to determine completely whether a given
construct is valid; it only determines how the syntax is parsed. Section
14, p1 says that:
"The _declaration_ in a _template-declaration_ shall
-- declare or define a function or a class, or
-- define a member function, a member class, or a static data member of
a class template or of a class nested withing a class template, or
-- define a member template of a class or a class template"
Your example doesn't fit any of these categories.
> compiler should accept it, yet none do! Here's a workaround
> template <class T> struct List2 { typedef List<List<T>> type; };
That is the correct way to do it.
---
[ 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 ]