34

Is it possible to use custom allocator for std::vector internal allocations? If yes, how?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
Cartesius00
  • 23,584
  • 43
  • 124
  • 195

1 Answers1

20

You basically have to implement your allocator type to conform to the Allocator concept.

The linked page lists all requirements of that type, but the core functionality is implemented in the allocate member function.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • 26
    could you give an example – serup May 15 '18 at 11:03
  • @serup Click on the link, it has tons of examples. – bitmask May 15 '18 at 11:38
  • 9
    This is exactly the reason why link only answers are *bad*! The page doesn't exist anymore. Pointing towards examples in the broken link is even worse. – dtell Jun 18 '18 at 12:03
  • 2
    At least I updated the link. The new page is referenced, as cppreference is a wiki. But your point is taken. – bitmask Jun 18 '18 at 12:29
  • 5
    Here is a [running example](https://godbolt.org/z/7fnj1rGr9) derived from the [reference](https://en.cppreference.com/w/cpp/named_req/Allocator). – schoetbi Oct 27 '21 at 05:49