Allocators

Discussion in 'C++' started by Karl Napf, Mar 25, 2010.

  1. Karl Napf

    Karl Napf Guest

    Hi, I'm wondering why allocators only have a "copy-construct" method,
    but no method for default construction. For large objects that shall
    be initialized using the default constructor, this seems to be quite
    inefficient since one first has to create a temporary object which is
    then copied to the allocated space. Any ideas how to avoid this?
    Thanks and regards, Karl.
    Karl Napf, Mar 25, 2010
    #1
    1. Advertising

  2. Pete Becker <> wrote:
    > Karl Napf wrote:
    >> Hi, I'm wondering why allocators only have a "copy-construct" method,
    >> but no method for default construction.

    >
    > Allocators do have a default constructor.


    I think he meant that allocators don't offer a way to default-construct
    an allocated object (rather than allocators not having a default
    constructor).
    Juha Nieminen, Mar 26, 2010
    #2
    1. Advertising

  3. Karl Napf

    Karl Napf Guest

    On 26 Mrz., 07:46, Juha Nieminen <> wrote:
    > Pete Becker <> wrote:
    > > Karl Napf wrote:
    > >> Hi, I'm wondering why allocators only have a "copy-construct" method,
    > >> but no method for default construction.

    >
    > > Allocators do have a default constructor.

    >
    >   I think he meant that allocators don't offer a way to default-construct
    > an allocated object (rather than allocators not having a default
    > constructor).


    Right, that's what I meant :)
    Karl Napf, Mar 26, 2010
    #3
  4. On Mar 26, 3:52 am, Karl Napf <> wrote:
    > On 26 Mrz., 07:46, Juha Nieminen <> wrote:
    >
    > > Pete Becker <> wrote:
    > > > Karl Napf wrote:
    > > >> Hi, I'm wondering why allocators only have a "copy-construct" method,
    > > >> but no method for default construction.

    >
    > > > Allocators do have a default constructor.

    >
    > >   I think he meant that allocators don't offer a way to default-construct
    > > an allocated object (rather than allocators not having a default
    > > constructor).

    >
    > Right, that's what I meant :)


    This will be corrected in C++0X. Allocators will be able to construct
    objects using any argument list (including an empty one), and the
    allocator author doesn't even have to implement it (reference
    [allocator.traits.members], it's pretty cool work by Pablo Halpern).

    -Howard
    Howard Hinnant, Mar 27, 2010
    #4
  5. Karl Napf

    James Kanze Guest

    On Mar 25, 9:20 pm, Karl Napf <> wrote:
    > I'm wondering why allocators only have a "copy-construct" method,
    > but no method for default construction.


    So that you can have collections of objects which don't support
    default construction.

    > For large objects that shall be initialized using the default
    > constructor, this seems to be quite inefficient since one
    > first has to create a temporary object which is then copied to
    > the allocated space.


    The way the STL is designed supposes efficient copy anyway. If
    you want objects to be efficiently stored in STL containers,
    provide them with efficient copy. (Add a level of indirection,
    etc.)

    --
    James Kanze
    James Kanze, Mar 28, 2010
    #5
    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. Ross Ridge
    Replies:
    2
    Views:
    880
    Dhruv
    Jul 7, 2003
  2. Richard Smith
    Replies:
    20
    Views:
    991
    Andy Sawyer
    Jul 21, 2003
  3. Mark A. Gibbs

    operator= in allocators

    Mark A. Gibbs, Apr 4, 2004, in forum: C++
    Replies:
    9
    Views:
    424
    Mark A. Gibbs
    Apr 4, 2004
  4. mar00ned

    Help with STL allocators

    mar00ned, Sep 24, 2004, in forum: C++
    Replies:
    6
    Views:
    449
    John Harrison
    Sep 24, 2004
  5. Ares Lagae

    Containers & Allocators

    Ares Lagae, Feb 15, 2005, in forum: C++
    Replies:
    6
    Views:
    399
    Ares Lagae
    Feb 21, 2005
Loading...

Share This Page