Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding an allocator to a C++ class template for shared memory object creation
    primarykey
    data
    text
    <p>In short, my question is: If you have class, <code>MyClass&lt;T&gt;</code>, how can you change the class definition to support cases where you have <code>MyClass&lt;T, Alloc&gt;</code>, similar to how, say, STL vector provides. </p> <p>I need this functionality to support an allocator for shared memory. Specifically, I am trying to implement a ring buffer in shared memory. Currently it has the following ctor:</p> <pre><code>template&lt;typename ItemType&gt; SharedMemoryBuffer&lt;ItemType&gt;::SharedMemoryBuffer( unsigned long capacity, std::string name ) </code></pre> <p>where <code>ItemType</code> is the type of the data to be placed in each slot of the buffer.</p> <p>Now, this works splendid when I create the buffer from the main program thus</p> <pre><code>SharedMemoryBuffer&lt;int&gt;* sb; sb = new SharedMemoryBuffer&lt;int&gt;(BUFFER_CAPACITY + 1, sharedMemoryName); </code></pre> <p>However, in this case the buffer itself is not created in shared memory and so is not accessible to other processes. What I want to do is to be able to do something like</p> <pre><code>typedef allocator&lt;int, managed_shared_memory::segment_manager&gt; ShmemAllocator; typedef SharedMemoryBuffer&lt;int, ShmemAllocator&gt; MyBuffer; managed_shared_memory segment(create_only, "MySharedMemory", 65536); const ShmemAllocator alloc_inst (segment.get_segment_manager()); MyBuffer *mybuf = segment.construct&lt;MyBuffer&gt;("MyBuffer")(alloc_inst); </code></pre> <p>However, I don't know how to go about adding an explicit allocator to the class template. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload