Skip to content

Instantly share code, notes, and snippets.

@EricWF
Last active December 27, 2016 19:14
Show Gist options
  • Select an option

  • Save EricWF/267d3268be5d69b668ef9c686e351ccb to your computer and use it in GitHub Desktop.

Select an option

Save EricWF/267d3268be5d69b668ef9c686e351ccb to your computer and use it in GitHub Desktop.
Fixed Size container example
using ValueType = /* ... */
// An allocator type which allocates from a fixed sized buffer provided upon construction.
// max_size() reports the number of elements which can be allocated in the remaining buffer space.
using Alloc = BufferAllocator<ValueType>;
// Any STL container.
using Container = std::container<ValueType, ..., Alloc>;
// node_storage_type is an extension I wish to propose which exposes the size and alignment
// of the internal node type as a specialization of aligned_storage_t<...>
using NodeStorageType = Container::node_storage_type;
// Possible use case.
void fillFixedSizedContainer(Container &C, ValueType const& VT = VT()) {
while (C.size() < C.max_size())
C.insert(VT);
}
int main() {
// Allocate all of the memory for the container on the stack.
std::array<NodeStorageType, 10> Buffer;
Container C(Alloc(&Buffer));
C.max_size(); // reports 10.
// Example usage
fillFixedSizeContainer(C);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment