I am working with boost multi index in shared memory
I create a struct SrHpackHeaderElement that contain shared strings
struct SrHpackHeaderElement
{
SrHpackHeaderElement(char_allocator* pCharAllocator):m_sName("",pCharAllocator),m_sValue("",pCharAllocator){}
shm_string m_sName;
shm_string m_sValue;
};
The same struct is used in a shared vector
typedef bip::managed_shared_memory::allocator<SrHpackHeaderElement>::type HpackHeader_allocator;
typedef bip::allocator<SrHpackHeaderElement, HpackHeader_allocator> HpackHeader_ShmemAllocator;
typedef bip::vector<SrHpackHeaderElement, HpackHeader_ShmemAllocator> HpackTableType;
struct CrHttp2HpackValue
{
HpackTableType m_table;
CrHttp2HpackValue(HpackHeader_ShmemAllocator * pHpackHeaderShmemAllocator,char_allocator* pCharAllocator):m_table(pHpackHeaderShmemAllocator)
{
}
};
I want to pass the allocator to the struct constructor CrHttp2HpackValue, and to initialize the shared/interprocess vector Is this possible?
I put the code in the following link https://coliru.stacked-crooked.com/a/46b96c0a70a165b9
I think the code is excessively complicated due to some specifics of C++ and allocators you're seemingly not taking advantage of:
new) and passed around by ref or value, in particular this is the case for segment managers and allocators (even though they're managing memory that is definitely not in the stack, but this they deal with internally).xof typewhatever_allocator<T>and need to construct an objectyof typewhatever_allocator<Q>you can simply writewhatever_allocator<Q> y = x. That is, allocators out of the same class template are interconvertible.With these features in mind, the code can we just rewritten as follows:
Live Coliru Demo