Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:56:59+00:00 2026-05-31T14:56:59+00:00

I like to know how things work and as such have been delving deep

  • 0

I like to know how things work and as such have been delving deep into the c++ standard library. Something occurred to me the other day.

It is required that containters (for example: std::vector<int, std::allocator<int> >) use the allocator specified for allocations. Specifically the standard says:

23.1.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. A copy of this argument is used for any memory
allocation performed, by these constructors and by all member
functions, during the lifetime of each container object. In all
container types defined in this clause, the member get_allocator()
returns a copy of the Allocator object used to construct the
container.

Also later in the standard it says (in a few different spots but i’ll pick one) things like this:

explicit deque(size_type n, const T& value = T(), const Allocator& = Allocator());

Effects: Constructs a deque with n copies of value,
using the specified allocator.

OK, so on to my question.

Let’s take std::vector as an example, the natural and efficient way to implement something like:

vector<T, A>::vector(const vector& x)

might look something like this:

template <class T, class A>
vector<T, A>::vector(const vector& x)  {
    pointer p = alloc_.allocate(x.size());
    std::uninitialized_copy(x.begin(), x.end(), p);
    first_ = p;
    last_  = p + x.size();
    end_   = p + x.size();
}

specifically, we allocate some memory and then copy construct all the members in place. Not bothering to do something like new value_type[x.size()] because that would default construct the array only to overwrite it!.

but, this doesn’t use the allocator to do the copy construction…

I could manually write a loop which does something like this:

while(first != last) {
    alloc_.construct(&*dest++, *first++);
}

but that’s a waste, it’s nearly identical to std::uninitialized_copy, the only difference is that is uses the allocator instead of placement new.

So, would you consider it an oversight that the standard doesn’t have the (seemingly obvious to me) set of functions like these:

template <class In, class For, class A>
For uninitialized_copy(In first, In last, For dest, A &a);

template <class In, class Size, class For, class A>
For uninitialized_copy_n(In first, Size count, For dest, A &a);

template <class For, class T, class A>
void uninitialized_fill(For first, For last, const T& x, A &a);

template <class For, class Size, class T, class A>
void uninitialized_fill_n(For first, Size count, const T& x, A &a);

I would imagine that these types of functions (even though they are trivial to implement manually… until you try to make them exception safe) would prove fairly useful if people want to implement there own containers and such and make efficient use of copy construction while using allocators.

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:57:01+00:00Added an answer on May 31, 2026 at 2:57 pm

    I’m not sure whether we could call it an “oversight”, per se.

    No, you can’t provide your own allocator to these specialised algorithms. But then there are other things that the standard doesn’t contain, either.

    @MarkB identifies a very good reason that the standard shouldn’t do this (that the range has no knowledge of the container’s allocator). I’d go so far as to say it’s just an inherent limitation.

    You can always re-invent uninitialized_copy for your needs, knowing what the allocator should be. It’s just a two-line for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to know when i should and shouldn't be wrapping things in a
Hi I'm new to SVN and would like to know a couple of things
Does anyone know (like in tried and succeeded) if I can have D2009 both
I have a Silverlight 3 project with something like this: <ItemGroup> <Content Include=Content\image1.png> </Content>
I know this has been asked before. But I have exhausted the options given
I have a script element in my webpage, something like this: <script id=myscript></script> Now,
I have been pondering writing this question for quite some time. I work for
I really have little idea how ACL do work. I know it's pretty cool
I like vectors a lot. They're nifty and fast. But I know this thing
I'd like know what people think about using RAISERROR in stored procedures to pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.