Topic: is allocator<> standard?


Author: llewelly.@@edevnull.dot.com
Date: Tue, 5 Sep 2000 17:39:13 GMT
Raw View
Erik Haugen <haugen@Xenon.Stanford.EDU> writes:

> should this compile everywhere:
> -----------------
> #include <memory>
> #include <vector>
>
> using namespace std;
>
> template<class T>
> class suballocator: public allocator<T>
> {
>         void destroy(pointer p)
>         {
>   // do something special on destroying only
>         }
> };
>
>
> int main()
> {
>  vector<int, suballocator<int> > vec;
>
>         return 0;
> }
> ------------------

I believe this is well-formed.

>
> I'm trying to use a different allocator with the std::vector class - can I
> subclass off the standard allocator, or would I have to just write my own
> without subclassing, in order to be portable?
>
> g++ doesn't seem to have an allocator class; I'm wondering if that is just
> another example of g++ being flagrantly non-compliant.

Get a newer version of g++. I have g++ 2.95.2, and your example
  compiles fine for me.

[snip]

---
[ 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: "Ken Bloom" <ken195@bigfoot.com>
Date: Tue, 5 Sep 2000 17:39:25 GMT
Raw View
Erik Haugen <haugen@Xenon.Stanford.EDU> wrote in message
news:Pine.GSO.4.21.0009021636360.14977-100000@Xenon.Stanford.EDU...
> should this compile everywhere:
> -----------------
> #include <memory>
> #include <vector>
>
> using namespace std;
>
> template<class T>
> class suballocator: public allocator<T>
> {
>         void destroy(pointer p)
>         {
> // do something special on destroying only
>         }
> };
>
>
> int main()
> {
> vector<int, suballocator<int> > vec;
>
>         return 0;
> }
> ------------------
>
> I'm trying to use a different allocator with the std::vector class - can I
> subclass off the standard allocator, or would I have to just write my own
> without subclassing, in order to be portable?
>
> g++ doesn't seem to have an allocator class; I'm wondering if that is just
> another example of g++ being flagrantly non-compliant.
>

The standard does require a std::allocator<> class. This will work on any
standards conforming compiler.
As for whether or not it's ethical or a good idea, see consult your own
opinions about concrete versus abstract classes and act accordingly. Many
people here will try to impose their opinion about this issue on you (things
about inheriting from STL containers, etc...) but I trust you know what
you're doing.

--
Ken Bloom

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/M/AT/U d- s++:--- a--- C++ UL P++ L+ E----
W+++ N++ ?o ?K w++ !O M++>$ V- PS PE Y-- PGP- t+
5 X++ R--- tv-- b++ DI+ D-- G e- !h r--@ y?
------END GEEK CODE BLOCK------



---
[ 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: Tue, 5 Sep 2000 17:39:46 GMT
Raw View
"llewelly."@@edevnull.dot.com wrote:
>
> Erik Haugen <haugen@Xenon.Stanford.EDU> writes:
>
> > should this compile everywhere:
> > -----------------
> > #include <memory>
> > #include <vector>
> >
> > using namespace std;
> >
> > template<class T>
> > class suballocator: public allocator<T>
> > {
> >         void destroy(pointer p)
> >         {
> >               // do something special on destroying only
> >         }
> > };
>
> You are missing a few member functions; see 20.1.5, particularly table 32.

Almost all the members needed are either publicly inherited from
std::allocator, or are special member functions which are implicitly
declared and implicitly defined in what "happens" to be precisely the
right way. That's NOT a coincidence - a lot of good language design went
into the implicit definition rules. The only members that must be
explicitly provided are the ones that don't work properly when inherited
or implicitly declared and implicitly defined.

Of course, the special thing he wants to do with destroy might require
creating some suballocator members which hide (and perhaps use) the
corresponding std::allocator members. However, since he didn't specify
what that special thing is, the only necessary ones are 'rebind' and the
templated conversion constructor, as far as I can tell.

---
[ 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: Erik Haugen <haugen@Xenon.Stanford.EDU>
Date: Sun, 3 Sep 2000 12:30:42 GMT
Raw View
should this compile everywhere:
-----------------
#include <memory>
#include <vector>

using namespace std;

template<class T>
class suballocator: public allocator<T>
{
        void destroy(pointer p)
        {
  // do something special on destroying only
        }
};


int main()
{
 vector<int, suballocator<int> > vec;

        return 0;
}
------------------

I'm trying to use a different allocator with the std::vector class - can I
subclass off the standard allocator, or would I have to just write my own
without subclassing, in order to be portable?

g++ doesn't seem to have an allocator class; I'm wondering if that is just
another example of g++ being flagrantly non-compliant.

thanks,
Erik


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