Topic: Specialization of std::vector


Author: Anatoli <REMOVETHIS.anatoli@ptc.com>
Date: 1998/10/20
Raw View
Ryszard Kabatek wrote:
>
> I try to write a specialization of the std::vector class.
[snip]
> # include <vector>
>
> template <class T /*, class Alloc = alloc*/>
> class vector<T* /*, Alloc*/> : private vector<void* /*, Alloc*/>

Here's the problem.  The compiler attempts to
instantiate vector<void*> using template <class T> class vector<T*>,
which inherits from vector<void*>, which is to be instantiated
using template <class T> class vector<T*>, which inherits...

You need to instantiate vector<void*> explicitly before trying
to define template <class T> class vector<T*>.  Like this:

   template class vector <void*>;

   template <class T>
   class vector <T*> : private vector <void *>
     ...
--
Regards
Anatoli (anatoli at ptc dot com) -- opinions aren'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: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1998/10/21
Raw View
Nathan Myers wrote in message <70ie7o$le6$1@shell7.ba.best.com>...
>
>2. You cannot specialize standard classes on a built-in type, only
>   on your own types.

I've never heard of this restriction, it works for me :-)
---
[ 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: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
Date: 1998/10/21
Raw View
Anatoli wrote:
>
> You need to instantiate vector<void*> explicitly before trying
> to define template <class T> class vector<T*>.  Like this:
>
>    template class vector <void*>;
>
>    template <class T>
>    class vector <T*> : private vector <void *>
>      ...
> --
> Regards
> Anatoli (anatoli at ptc dot com) -- opinions aren't

It does not work. There may be only one explicit instantiation.

I'm supprised, the language does not allow such simple solution
I showed in my previous posting :-(

I tried:

# include <vector>

template <class T> class vector_p {}; // No members

template <>
class vector_p<void*> : public vector<void*> {
    typedef vector<void*> inherited;
//...
};

template <class T>
class vector<T*> : private vector_p<void*> {
    typedef vector_p<void*> inherited;
//...
};


It does work for any pointer type, for `void*' too!

But it does not work with the Alloc template parameter.
I cannot understand this behaviour.


Ryszard Kabatek
--
Martin-Luther University Halle-Wittenberg
Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129
---
[ 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: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
Date: 1998/10/21
Raw View
Nathan Myers wrote:
>
> 3. You probably should get the 3.11 version of the SGI STL.

I rather will wait for libstdc++-v3.


Ryszard Kabatek
--
Martin-Luther University Halle-Wittenberg
Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129


[ 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: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
Date: 1998/10/20
Raw View
I try to write a specialization of the std::vector class.

In both cases, with and without the Alloc template parameter
I get the error message:
base class `vector<void *,__default_alloc_template<true,0> >'
has incomplete type

Is this a correct behaviour of the compiler?

I'm using the egcs-1.1 with the SGI's STL 3.1.



# include <vector>

template <class T /*, class Alloc = alloc*/>
class vector<T* /*, Alloc*/> : private vector<void* /*, Alloc*/>
{};

int main()
{
  vector<int*> v;
}


Ryszard Kabatek
--
Martin-Luther University Halle-Wittenberg
Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129


[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/10/20
Raw View
Ryszard Kabatek <kabatek@chemie.uni-halle.de> wrote:
>
>I try to write a specialization of the std::vector class. ...
>
>template <class T /*, class Alloc = alloc*/>
>class vector<T* /*, Alloc*/> : private vector<void* /*, Alloc*/>
>{};

1. Your "specialization" is no more specialized than the class definition
   itself.  A specialization must actually bind something.
2. You cannot specialize standard classes on a built-in type, only
   on your own types.
3. You probably should get the 3.11 version of the SGI STL.

--
Nathan Myers
ncm@nospam.cantrip.org  http://www.cantrip.org/



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