Using STL Allocator with STL Vectors

Here is the main problem. There's an API I'm dependent on using a method using the following syntax:

void foo_api (std::vector<type>& ref_to_my_populated_vector);

The area of ​​code in question is quite intense, and I want to avoid using heaps to allocate memory. As a result, I created a special allocator that allocates the memory needed for the vector on the stack. So now I can define the vector as:

// Create the stack allocator, with room for 100 elements
my_stack_allocator<type, 100> my_allocator;

// Create the vector, specifying our stack allocator to use
std::vector<type, my_stack_allocator> my_vec(my_allocator);

Everything is fine. Performance tests using a dedicated stack vector compared to the standard vector show metric are about 4 times faster. The problem is that I cannot call foo_api! So that...

foo_api(my_vec); // Results in an error due to incompatible types.
// Can't convert std::vector<type> to std::vector<type, allocator>

Is there a solution?

+3
source share
1

, . , .

reserve, , .

, . . , ; !

, - operator new kin . , . 512 4 ( ); )

+3

Source: https://habr.com/ru/post/1729496/


All Articles