allocator requirements

Discussion in 'C++' started by John Harrison, Jul 9, 2004.

  1. If you specify an allocator in an STL container is it a requirement that
    the allocator allocates object of the right type, or can you assume that
    the container will rebind the allocator to the correct type?

    For example I tried the following code which uses a 'wrong' allocator and
    was slightly surrised to find it compiles on the three compilers I tried
    it on

    #include <vector>
    #include <memory>

    int main()
    {
    std::vector<int, std::allocator<double> > vec;
    vec.push_back(1);
    }

    john
     
    John Harrison, Jul 9, 2004
    #1
    1. Advertising

  2. John Harrison wrote:
    > If you specify an allocator in an STL container is it a requirement
    > that the allocator allocates object of the right type, or can you
    > assume that the container will rebind the allocator to the correct type?


    It's quite possible that the container uses 'construct' member
    function, which accepts the memory of any type (for placement
    new) and double* is good enough for int values to be constructed
    in. You probably get some excessive memory use that way, but
    not any serious problem. Try it in reverse, have a vector of
    double and use allocator<char> just for kicks...

    As to 'rebind', I can't find any reference to it in the chapter
    on containers. Assume you can, rely upon... I wouldn't.

    >
    > For example I tried the following code which uses a 'wrong' allocator
    > and was slightly surrised to find it compiles on the three compilers I
    > tried it on
    >
    > #include <vector>
    > #include <memory>
    >
    > int main()
    > {
    > std::vector<int, std::allocator<double> > vec;
    > vec.push_back(1);
    > }


    V
     
    Victor Bazarov, Jul 9, 2004
    #2
    1. Advertising

  3. On Fri, 09 Jul 2004 20:20:34 GMT, Victor Bazarov <>
    wrote:

    > John Harrison wrote:
    >> If you specify an allocator in an STL container is it a requirement
    >> that the allocator allocates object of the right type, or can you
    >> assume that the container will rebind the allocator to the correct
    >> type?

    >
    > It's quite possible that the container uses 'construct' member
    > function, which accepts the memory of any type (for placement
    > new) and double* is good enough for int values to be constructed
    > in. You probably get some excessive memory use that way, but
    > not any serious problem. Try it in reverse, have a vector of
    > double and use allocator<char> just for kicks...
    >
    > As to 'rebind', I can't find any reference to it in the chapter
    > on containers. Assume you can, rely upon... I wouldn't.
    >
    >> For example I tried the following code which uses a 'wrong' allocator
    >> and was slightly surrised to find it compiles on the three compilers I
    >> tried it on
    >> #include <vector>
    >> #include <memory>
    >> int main()
    >> {
    >> std::vector<int, std::allocator<double> > vec;
    >> vec.push_back(1);
    >> }

    >
    > V


    As a corrolary I tried the following

    #include <iostream>
    #include <vector>
    #include <memory>

    typedef std::vector<int, std::allocator<double> >::allocator_type X;

    void test(std::allocator<double> const&)
    {
    std::cout << "double\n";
    }

    void test(std::allocator<int> const&)
    {
    std::cout << "int\n";
    }

    int main()
    {
    test(X());
    }

    What would you expect the output to be, 'int' or 'double'? Both compilers
    I tried it on the output was 'int'. Which means those versions of the STL
    are rebinding the allocator to be an int allocator.

    john
     
    John Harrison, Jul 9, 2004
    #3
  4. "John Harrison" <> wrote in message
    news:eek:psavy7sdb212331@andronicus...
    > If you specify an allocator in an STL container is it a requirement

    that
    > the allocator allocates object of the right type, or can you assume

    that
    > the container will rebind the allocator to the correct type?


    I think this is addressed by 23.1 [lib.container.requirements] para.
    8:

    Copy constructors for all container types defined in this clause copy
    an allocator argument from their
    respective first parameters. All other constructors for these
    container types take an Allocator& argument
    (20.1.5), an allocator whose value type is the same as the container's
    value type.

    Jonathan
     
    Jonathan Turkanis, Jul 9, 2004
    #4
  5. John Harrison

    P.J. Plauger Guest

    "John Harrison" <> wrote in message
    news:eek:psavy7sdb212331@andronicus...

    > If you specify an allocator in an STL container is it a requirement that
    > the allocator allocates object of the right type,


    Yes, according to the C++ Standard.

    > or can you assume that
    > the container will rebind the allocator to the correct type?


    Yes, according to widespread practice.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Jul 9, 2004
    #5
  6. On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <> wrote:

    > "John Harrison" <> wrote in message
    > news:eek:psavy7sdb212331@andronicus...
    >
    >> If you specify an allocator in an STL container is it a requirement that
    >> the allocator allocates object of the right type,

    >
    > Yes, according to the C++ Standard.
    >
    >> or can you assume that
    >> the container will rebind the allocator to the correct type?

    >
    > Yes, according to widespread practice.
    >
    > P.J. Plauger
    > Dinkumware, Ltd.
    > http://www.dinkumware.com
    >


    Yes it seems so, both implementations of the STL I've checked do rebind
    the allocator for vector at least. This seems to directly contradict the
    requirements for allocator_type.

    I also noticed that Josuttis' book also ignores what the standard says,
    see first page of chapter 15 where he happily passes the same allocator to
    several containers with different value types.

    What's the reason for the standard saying what it does? It seems a bit
    inconvenient.

    john
     
    John Harrison, Jul 9, 2004
    #6
  7. "John Harrison" <> wrote in message
    news:eek:psav1xfa5212331@andronicus...
    > On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <>

    wrote:
    >
    > > "John Harrison" <> wrote in message
    > > news:eek:psavy7sdb212331@andronicus...
    > >
    > >> If you specify an allocator in an STL container is it a

    requirement that
    > >> the allocator allocates object of the right type,

    > >
    > > Yes, according to the C++ Standard.
    > >
    > >> or can you

    assume that
    > >> the container will rebind the allocator to the correct type?

    > >
    > > Yes, according to widespread practice.
    > >
    > > P.J. Plauger
    > > Dinkumware, Ltd.
    > > http://www.dinkumware.com
    > >

    >
    > Yes it seems so, both implementations of the STL I've checked do

    rebind
    > the allocator for vector at least. This seems to directly contradict

    the
    > requirements for allocator_type.


    How does this contradict the allocator requirements?

    > I also noticed that Josuttis' book also ignores what the standard

    says,
    > see first page of chapter 15 where he happily passes the same

    allocator to
    > several containers with different value types.


    It looks to me like the allocators he uses all have value_types
    appropriate for the containers.

    Jonathan
     
    Jonathan Turkanis, Jul 9, 2004
    #7
  8. On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
    <> wrote:

    >
    > "John Harrison" <> wrote in message
    > news:eek:psav1xfa5212331@andronicus...
    >> On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <>

    > wrote:
    >>
    >> > "John Harrison" <> wrote in message
    >> > news:eek:psavy7sdb212331@andronicus...
    >> >
    >> >> If you specify an allocator in an STL container is it a

    > requirement that
    >> >> the allocator allocates object of the right type,
    >> >
    >> > Yes, according to the C++ Standard.
    >> >
    >> >> or can you

    > assume that
    >> >> the container will rebind the allocator to the correct type?
    >> >
    >> > Yes, according to widespread practice.
    >> >
    >> > P.J. Plauger
    >> > Dinkumware, Ltd.
    >> > http://www.dinkumware.com
    >> >

    >>
    >> Yes it seems so, both implementations of the STL I've checked do

    > rebind
    >> the allocator for vector at least. This seems to directly contradict

    > the
    >> requirements for allocator_type.

    >
    > How does this contradict the allocator requirements?


    Not the allocator requirements, the requirements for allocator_type. For
    instance 23.2.4 has

    template <class T, class Allocator = std::allocator<T> >
    class vector
    {
    ...
    typedef Allocator allocator_type;

    i.e. allocator_type should be the same type as the template parameter.

    >
    >> I also noticed that Josuttis' book also ignores what the standard

    > says,
    >> see first page of chapter 15 where he happily passes the same

    > allocator to
    >> several containers with different value types.

    >
    > It looks to me like the allocators he uses all have value_types
    > appropriate for the containers.
    >


    Well maybe I'm reading too much into this but in chpater 15 in quick
    succession he gives

    vector<int,SpecialAlloc> v;

    map<int,float,less<int>,SpecialAlloc> m;

    basic_string<char,char_traits<char>,SpecialAlloc> s;

    Its not specified anywhere what SpecialAlloc is but by the quote you made
    from the standard it cannot have the correct value type for all these
    different containers.

    john

    > Jonathan
    >
    >
     
    John Harrison, Jul 9, 2004
    #8
  9. John Harrison

    P.J. Plauger Guest

    "John Harrison" <> wrote in message
    news:eek:psav1xfa5212331@andronicus...

    > On Fri, 09 Jul 2004 20:47:16 GMT, P.J. Plauger <> wrote:
    >
    > > "John Harrison" <> wrote in message
    > > news:eek:psavy7sdb212331@andronicus...
    > >
    > >> If you specify an allocator in an STL container is it a requirement

    that
    > >> the allocator allocates object of the right type,

    > >
    > > Yes, according to the C++ Standard.
    > >
    > >> or can you assume that
    > >> the container will rebind the allocator to the correct type?

    > >
    > > Yes, according to widespread practice.
    > >
    > > P.J. Plauger
    > > Dinkumware, Ltd.
    > > http://www.dinkumware.com
    > >

    >
    > Yes it seems so, both implementations of the STL I've checked do rebind
    > the allocator for vector at least. This seems to directly contradict the
    > requirements for allocator_type.


    I think this approach gives meaning to an otherwise ill formed program,
    hence it's a conforming implementation.

    > I also noticed that Josuttis' book also ignores what the standard says,
    > see first page of chapter 15 where he happily passes the same allocator to
    > several containers with different value types.
    >
    > What's the reason for the standard saying what it does? It seems a bit
    > inconvenient.


    It was simple, and the rebind mechanism was introduced as a cutesy
    trick before template template was well established in the language.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Jul 9, 2004
    #9
  10. "John Harrison" <> wrote in message
    news:eek:psav3hke1212331@andronicus...
    > On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
    > <> wrote:
    >
    > >
    > > "John Harrison" <> wrote in message
    > > news:eek:psav1xfa5212331@andronicus...


    > >>
    > >> Yes it seems so, both implementations of the STL I've checked do

    > > rebind
    > >> the allocator for vector at least. This seems to directly

    contradict
    > > the
    > >> requirements for allocator_type.

    > >
    > > How does this contradict the allocator requirements?

    >
    > Not the allocator requirements,


    Sorry, I read your post too fast.

    >the requirements for allocator_type. For
    > instance 23.2.4 has
    >
    > template <class T, class Allocator = std::allocator<T> >
    > class vector
    > {
    > ...
    > typedef Allocator allocator_type;
    >
    > i.e. allocator_type should be the same type as the template

    parameter.

    I still don't see the violation.

    > >
    > >> I also noticed that Josuttis' book also ignores what the standard

    > > says,
    > >> see first page of chapter 15 where he happily passes the same

    > > allocator to
    > >> several containers with different value types.

    > >
    > > It looks to me like the allocators he uses all have value_types
    > > appropriate for the containers.
    > >

    >
    > Well maybe I'm reading too much into this but in chpater 15 in quick
    > succession he gives
    >
    > vector<int,SpecialAlloc> v;
    >
    > map<int,float,less<int>,SpecialAlloc> m;
    >
    > basic_string<char,char_traits<char>,SpecialAlloc> s;


    That's funny. My copy has

    vector<int, MyAlloc<int> > v;
    map<int,float,less<int>,MyAlloc<std::pair<const int, float> > > m;
    ...

    Jonathan
     
    Jonathan Turkanis, Jul 9, 2004
    #10
  11. Jonathan Turkanis wrote:
    > "John Harrison" <> wrote in message
    > news:eek:psav3hke1212331@andronicus...
    >
    >>On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis
    >><> wrote:
    >>
    >>
    >>>"John Harrison" <> wrote in message
    >>>news:eek:psav1xfa5212331@andronicus...

    > [...]
    >>>>I also noticed that Josuttis' book also ignores what the standard
    >>>
    >>>says,
    >>>
    >>>>see first page of chapter 15 where he happily passes the same
    >>>
    >>>allocator to
    >>>
    >>>>several containers with different value types.
    >>>
    >>>It looks to me like the allocators he uses all have value_types
    >>>appropriate for the containers.
    >>>

    >>
    >>Well maybe I'm reading too much into this but in chpater 15 in quick
    >>succession he gives
    >>
    >>vector<int,SpecialAlloc> v;
    >>
    >>map<int,float,less<int>,SpecialAlloc> m;
    >>
    >>basic_string<char,char_traits<char>,SpecialAlloc> s;

    >
    >
    > That's funny. My copy has
    >
    > vector<int, MyAlloc<int> > v;
    > map<int,float,less<int>,MyAlloc<std::pair<const int, float> > > m;
    > ...


    <Butting in...>

    So does mine, but it's the 7th printing (what's yours?) so, either
    before they printed something differently or after...

    V
     
    Victor Bazarov, Jul 9, 2004
    #11
  12. "Victor Bazarov" <> wrote in message
    news:uvEHc.1987$...
    > Jonathan Turkanis wrote:
    > > "John Harrison" <> wrote in message
    > > news:eek:psav3hke1212331@andronicus...


    > >>Well maybe I'm reading too much into this but in chpater 15 in

    quick
    > >>succession he gives
    > >>
    > >>vector<int,SpecialAlloc> v;
    > >>
    > >>map<int,float,less<int>,SpecialAlloc> m;
    > >>
    > >>basic_string<char,char_traits<char>,SpecialAlloc> s;

    > >
    > >
    > > That's funny. My copy has
    > >
    > > vector<int, MyAlloc<int> > v;
    > > map<int,float,less<int>,MyAlloc<std::pair<const int, float> > >

    m;
    > > ...

    >
    > <Butting in...>
    >
    > So does mine, but it's the 7th printing (what's yours?) so, either
    > before they printed something differently or after...


    Mine is the 10th. I wonder if Josuttis changed it because he decided
    it was an error or because he thought it was debatable or an
    unnecessary distraction.

    I'm a bit suprised that the requirement that the value_type's match is
    stated clearly for basic_string (21.3/1) but only in an almost
    parenthetical way for containers generally (23.1/8), AFAICT.

    Jonathan
     
    Jonathan Turkanis, Jul 9, 2004
    #12
  13. >>
    >> Yes it seems so, both implementations of the STL I've checked do rebind
    >> the allocator for vector at least. This seems to directly contradict the
    >> requirements for allocator_type.

    >
    > I think this approach gives meaning to an otherwise ill formed program,
    > hence it's a conforming implementation.
    >


    Yes, thinking about it I agree.

    john
     
    John Harrison, Jul 10, 2004
    #13
  14. On Fri, 9 Jul 2004 16:13:19 -0600, Jonathan Turkanis
    <> wrote:

    >>

    > Mine is the 10th. I wonder if Josuttis changed it because he decided
    > it was an error or because he thought it was debatable or an
    > unnecessary distraction.
    >


    Mine's the third, the change is listed in the errata on his website.

    john
     
    John Harrison, Jul 10, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    0
    Views:
    489
  2. Fraser Ross

    allocator size_type

    Fraser Ross, Jul 31, 2003, in forum: C++
    Replies:
    1
    Views:
    452
    John Harrison
    Jul 31, 2003
  3. =?ISO-8859-1?Q?Ralf_Schneewei=DF?=

    How to write an allocator for the STL List in VC++ 6.0

    =?ISO-8859-1?Q?Ralf_Schneewei=DF?=, Aug 20, 2003, in forum: C++
    Replies:
    2
    Views:
    621
    Shane Beasley
    Aug 21, 2003
  4. Alexander Stippler

    allocator traits?

    Alexander Stippler, Sep 2, 2003, in forum: C++
    Replies:
    4
    Views:
    400
    Alexander Stippler
    Sep 3, 2003
  5. Brian Genisio
    Replies:
    12
    Views:
    8,153
    tom_usenet
    Jan 15, 2004
Loading...

Share This Page