Answer the question
In order to leave comments, you need to log in
How to calculate required buffer size for monotonic_buffer_resource?
After all, not only data is written to this buffer, but also pointers to the next / previous nodes and something else, if the container needs it. Is it possible to somehow get the size of the "node" from the container in runtime/compiletime in order to multiply it by the expected number of elements and make a buffer of the required size? A node is not an object with stored data, but user data + service data necessary to implement the container.
constexpr int total_nodes = 10;
// Нужно создать буфер достаточного размера, при этом это не sizeof(int)*total_nodes
constexpr int enoughSize = total_nodes * ???;// enough to fit in all nodes
std::array<std::byte, enoughSize> buffer;
buffer.fill( std::byte{0} );
std::pmr::monotonic_buffer_resource mbr{buffer.data(), buffer.size()};
std::pmr::polymorphic_allocator<int> pa{&mbr};
std::pmr::list<int> list{pa};
for( int i=0; i < total_nodes; ++i )
{
list.push_back( i );
}
std::array<std::byte, enoughSize> buffer;
std::pmr::monotonic_buffer_resource mbr{buffer.data(), buffer.size()};
std::pmr::polymorphic_allocator<int> pa{&mbr};
std::pmr::map<int,int> map{pa};
map[1]=1;
map[2]=2;
Answer the question
In order to leave comments, you need to log in
For example, std::vector defines the type value_type - this is just the type of the element of the vector.
You can use constructs like:
sizeof(decltype(vector_val)::value_type)
или
sizeof(decltype(vector_val.back()))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question