Reputation: 423
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?
Upvotes: 0
Views: 114
Reputation: 101
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.
Upvotes: -3