0

I am using an external library, and they have a particular convention with their classes that makes no sense to me. They will declare them like this:

class SomeClass
{
public:
    using allocator_type = ::std::allocator<uint8_t>;

    SomeClass() noexcept :
            SomeClass(allocator_type())
    {}

    explicit SomeClass(const allocator_type& allocator) noexcept;

    ...

They take the default allocator as an argument for internal allocation. But the default allocator is not customizable, I don't see how I could control the memory at all. It is stateless, and will always allocate globally with new(). It seems like they should have used an interface with virtual functions, or a templated class to be defined by the user.

This convention seems completely pointless, which makes me feel like I must be missing something. Am I right about this, or is there some purpose here that I don't get?

3
  • 3
    It's so that your class have the same API as classes which do have meaningful allocators e.g. std::vector. Commented Sep 27, 2024 at 4:55
  • 3
    It might help answer if you mention which external library it is. Commented Sep 27, 2024 at 6:40
  • 4
    Why questions about details are hard to answer out of context. Can be for to work together with some template that expects to pass allocator to constructor. Easiest way to find out is to modify and see what all blows up. Commented Sep 27, 2024 at 8:48

1 Answer 1

-3

It gives the flexibility of how you want to collect and return memory to the system. There could be use cases where one would reuse the previously allocated memory or define a specified alignment requirements or call a more optimized memory allocation OS interface. It basically abstracts away low level memory allocation calls through a nice interface and provides lots of customization points and error handling.

Sign up to request clarification or add additional context in comments.

2 Comments

A generic allocator might allow this (and OP is aware of this!), but std::allocator does not.
I think the library is trying to enforce that one does the allocation in terms of their allocator that only does allocation in terms of uint8_t.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.