Topic: Q: Nested templates?


Author: James Kuyper <kuyper@wizard.net>
Date: 1998/05/22
Raw View
Cristian Georgescu wrote:
>
> I am trying to provide a generic function that would take a parametrized
> collection of elements and the element type, f.i.:
>
> template <Class Collection>
> template <Class Element>
> void AddElement(Collection<Element>& collection, anElement)
> {
>         collection.push_back(anElement);
> };
>
> template <Class Collection, Class Element>
> void AddElement(Collection<Element>& collection, Element anElement)
> {
>         collection.push_back(anElement);
> };
>
> Which is the correct form?
> The VC5.3 doesn't seem to like either one.

C++ is a case sensitive language, and I'm pretty sure that 'class' must
be spelled in all lowercase letters. I hope so, because I've been
thinking about creating a template class named Class<T>, for creating a
wrapper class for non-class types.
---
[ 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: Petter Urkedal <petter@matfys.lth.se>
Date: 1998/05/22
Raw View
Cristian Georgescu wrote:
>
> I am trying to provide a generic function that would take a parametrized
> collection of elements and the element type, f.i.:
>
 [...]
>
> template <Class Collection, Class Element>
> void AddElement(Collection<Element>& collection, Element anElement)
> {
>         collection.push_back(anElement);
> };
>
> Which is the correct form?
> The VC5.3 doesn't seem to like either one.

In your example, Collection determined to be a type in the template
parameter list, but later used as a template name (which can only be
*made into* a type by applying actual template arguments).  The
first line should read

 template< template<class T> class Collection, class Element >

Isn't that logical? ;-)

Unfortunately, this is not supported by all compilers, so you may
have to be satisfied with less type-checking, as in

 template< class Collection, class Element >

Petter.
--

[- http://matfys.lth.se/~petter/ -]


[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/05/23
Raw View
Cristian Georgescu wrote:
>
> I am trying to provide a generic function that would take a parametrized
> collection of elements and the element type, f.i.:
>
> template <Class Collection>
> template <Class Element>
> void AddElement(Collection<Element>& collection, anElement)
> {
>         collection.push_back(anElement);
> };
>
> template <Class Collection, Class Element>
> void AddElement(Collection<Element>& collection, Element anElement)
> {
>         collection.push_back(anElement);
> };
>
> Which is the correct form?
> The VC5.3 doesn't seem to like either one.

Either:

template<class Collection, class Element>
 void AddElement(Collection& collection, Element anElement)
{
  collection.push_back(anElement);
}

or (if your compiler supports template template parameters):

template<template<class T> class Collection, class Element)
 void AddElement(Collection<Element>& collection, Element anElement)
{
  collection.push_back(anElement);
}

Note however that the seemingly better typesafety of the second
is really a restriction on the tallowed ype: While the first one
lets check the push_back member for the correct type, it allows
implicit conversion (since push_back itself is not templated over
its parameter), while the second option effectively disables
implicit conversions (since they are not allowed on templated
parameters, and the template serves for a second parameter
which exactly matches the type the container holds). For example

vector<float> v;
AddElement(v, 3.0);

will fail with the second option, since 3.0 is double, and the
only functions which may be generated are
AddElement(vector<float>, float) and AddElement(vector<double>,double),
and with an exact match required, both are not allowed.

However the first one will happily work by generating
AddElement(vector<float>, double) and doing the conversion on
the call of vector<float>::push_back(double const&).

While the first version seems less typesafe at the first view,
it isn't really: If you try to call f.ex. AddElement(v, (char*)NULL),
you won't get a function AddElement(vector<float>, char*), since
this would require calling vector<float>::push_back(float) with a
char*, and char* cannot be converted to float.
---
[ 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: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/05/23
Raw View
Cristian Georgescu <cgeorges@lehman.COM> wrote:
: I am trying to provide a generic function that would take a parametrized
: collection of elements and the element type, f.i.:

: template <Class Collection>
: template <Class Element>
: void AddElement(Collection<Element>& collection, anElement)
: {
:  collection.push_back(anElement);
: };


: template <Class Collection, Class Element>
: void AddElement(Collection<Element>& collection, Element anElement)
: {
:  collection.push_back(anElement);
: };

: Which is the correct form?

The second one. The first would be right for a member function
template of a class template.

: The VC5.3 doesn't seem to like either one.

Try lowercase ``C'' in the reserved word ``class''.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.


[ 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: Cristian Georgescu <cgeorges@lehman.COM>
Date: 1998/05/18
Raw View
I am trying to provide a generic function that would take a parametrized
collection of elements and the element type, f.i.:

template <Class Collection>
template <Class Element>
void AddElement(Collection<Element>& collection, anElement)
{
 collection.push_back(anElement);
};


template <Class Collection, Class Element>
void AddElement(Collection<Element>& collection, Element anElement)
{
 collection.push_back(anElement);
};

Which is the correct form?
The VC5.3 doesn't seem to like either one.
--
                      Cristian Georgescu
_________________________________________________
                      email: CGeorges@lehman.com
                      tel:   (212) 526-3502

    _/      _/_/_/_/  Lehman Brothers
   _/      _/    _/   Equity Finance Systems
  _/      _/_/_/_/    World Financial Center
 _/      _/    _/     200 Vesey Street
_/_/_/  _/_/_/_/      New York, NY 10285.
_________________________________________________



[ 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              ]