4 ms·
Ultimately this is why Halpern, Lakos, et al. pushed for polymorphic allocators to be added to C++. We make heavy use of them because it gives the caller the fu
by apaprocki 7y ago
Ultimately this is why Halpern, Lakos, et al. pushed for polymorphic allocators to be added to C++. We make heavy use of them because it gives the caller the full control over the allocator used in nearly all situations then the code is written properly to take and propagate allocators according to the rules.
(A little example posted by Bartlomiej Filipek showing a vector neatly using a stack buffer)
#include <iostream>
#include <memory_resource>
#include <vector>
int main() {
char buffer[64] = {};
std::fill_n(std::begin(buffer), std::size(buffer)-1, '_');
std::cout << buffer << '\n';
std::pmr::monotonic_buffer_resource pool{
std::data(buffer), std::size(buffer)
};
std::pmr::vector<char> vec{&pool};
for (char ch='a'; ch <= 'z'; ++ch)
vec.push_back(ch);
std::cout << buffer << '\n';
}
- saagarjha 7y agoI’m sorry, but I’m not seeing the point you’re trying to make from your example: std::vector takes a custom allocator as a template parameter already (also, the order of characters in implementation-specific in C++, FWIW).