#include <vector>
#include <cstdio>
struct item
{
char a;
int b;
};
template <typename T>
class Allocator
{
public:
using value_type = T;
using size_type = std::size_t;
public:
Allocator(T* buffer, std::size_t size)
: buffer_(buffer)
, size_(size)
{}
value_type* allocate(size_type n, const void* hint = 0) { return buffer_; }
void deallocate(T* p, size_type n) {}
private:
T* buffer_;
std::size_t size_;
};
int main()
{
item buffer[10] = {0};
Allocator<item> alloc(&buffer[0], sizeof(buffer));
std::vector<item, Allocator<item>> items(alloc);
{
item i = { 'a', 1};
items.emplace_back(i);
}
{
item i = { 'b', 2};
items.emplace_back(i);
}
//items.emplace_back('c', 3);
for (const char* p = reinterpret_cast<const char*>(buffer), *pend = p + (10 * sizeof(int)); p < pend; ++p) {
printf("%02x ", *p);
}
printf("\n");
}
Having a play with custom allocators.
The first two items are emplaced fine, but ideally one would use usual form which I have added to the program in a commented-out form.
If I enable that line, I get compiler errors.

