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 8578611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:25:49+00:00 2026-06-11T20:25:49+00:00

There is a template parameter for STL containers to chose a custom allocator. It

  • 0

There is a template parameter for STL containers to chose a custom allocator. It took a while, but I think I understand how it works. Somehow it isn’t really nice because the given allocator type isn’t used directly but it is rebound to the allocator of another type. Finally I can work with it.

After reading the API I recognized that there is also the possibility to give allocators as constructor parameter. But how do I know which kind of allocator the container uses, if it internally rebinds the given allocator from the template parameter?

Additionally I read that C++11 now uses scoped allocators which allow to reuse the allocator of a container for its containing containers. How does the implementation of a scoped allocator enabled container roughly differs from one that is not aware of scoped containers?

Unfortunately I wasn’t able to find anything that could explain this. Thanks for answers!

  • 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-06-11T20:25:51+00:00Added an answer on June 11, 2026 at 8:25 pm

    But how do I know which kind of allocator the container uses, if it
    internally rebinds the given allocator from the template parameter?

    Always supply an Allocator<T> to the constructor (where T is the value_type of the container). The container will convert it to an Allocator<U> is necessary where U is some internal data structure of the container. The Allocator is required to supply such converting constructors, e.g.:

    template <class T> class allocator {
        ...
        template <class U> allocator(const allocator<U>&);
    

    Additionally I read that C++11 now uses scoped allocators which allow
    to reuse the allocator of a container for its containing containers.

    Well, to be more precise, C++11 has an allocator adaptor called scoped_allocator_adaptor:

    template <class OuterAlloc, class... InnerAllocs>
    class scoped_allocator_adaptor : public OuterAlloc
    {
        ...
    };
    

    From C++11:

    The class template scoped_allocator_adaptor is an allocator template
    that specifies the memory resource (the outer allocator) to be used by
    a container (as any other allocator does) and also specifies an inner
    allocator resource to be passed to the constructor of every element
    within the container. This adaptor is instantiated with one outer and
    zero or more inner allocator types. If instantiated with only one
    alloca- tor type, the inner allocator becomes the
    scoped_allocator_adaptor itself, thus using the same allocator
    resource for the container and every element within the container and,
    if the elements themselves are con- tainers, each of their elements
    recursively. If instantiated with more than one allocator, the first
    allocator is the outer allocator for use by the container, the second
    allocator is passed to the constructors of the container’s elements,
    and, if the elements themselves are containers, the third allocator is
    passed to the elements’ elements, and so on. If containers are nested
    to a depth greater than the number of allocators, the last allocator
    is used repeatedly, as in the single-allocator case, for any remaining
    recursions. [Note: The scoped_allocator_adaptor is derived from the
    outer allocator type so it can be substituted for the outer allocator
    type in most expressions. — end note ]

    So you only get the scoped allocators behavior if you specify a scoped_allocator_adaptor as the allocator for your container.

    How does the implementation of a scoped allocator enabled container
    roughly differs from one that is not aware of scoped containers?

    The key is that the container now deals with its allocator via a new class called allocator_traits instead of dealing with the allocator directly. And the container must use allocator_traits for certain operations such as constructing and destructing value_types in the container. The container must not talk to the allocator directly.

    For example, allocators may provide a member called construct that will construct a type at a certain address using the given arguments:

    template <class T> class Allocator {
         ...
        template<class U, class... Args>
            void construct(U* p, Args&&... args);
    };
    

    If an allocator does not provide this member, allocator_traits will provide a default implementation. In any event, the container must construct all value_types using this construct function, but using it through allocator_traits, and not using the allocator directly:

    allocator_traits<allocator_type>::construct(the_allocator, *ugly details*);
    

    The scoped_allocator_adaptor provides custom construct functions which allocator_traits will forward to which take advantage of the uses_allocator traits and passes the correct allocator along to the value_type constructor. The container remains blissfully ignorant of these details. The container only has to know that it must construct the value_type using the allocator_traits construct function.

    There are more details the container must have to deal with to correctly handle stateful allocators. Though these details too are dealt with by having the container not make any assumptions but get all properties and behaviors via allocator_traits. The container can not even assume that pointer is T*. Rather this type is found by asking allocator_traits what it is.

    In short, to build a C++11 container, study up on allocator_traits. And then you get scoped allocator behavior for free when your clients use the scoped_allocator_adaptor.

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

Sidebar

Related Questions

I have a structure with a template parameter, Stream . Within that structure, there
Is there a way to define a custom template for the no results view
Is there a way to have the compile deduce the template parameter automatically? template<class
I'm implementing an STL set with a complex template parameter type. When inserting in
I have a class A that has a template parameter T. There are use
Are there existing template extract libraries in either python or php? Perl has Template::Extract
Are there a javascript template engine which supports formatters? That is, I can tell
Are there any VS template/Starter Kit/Any example of an ASP.NET Web Application, which uses
There were many questions about C++ template classes which contain static member variables, as
there is insert iterator in database template library or other library, Can someone tell

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.