This example shows how to create an allocator-aware type, which takes an allocator and can be stored inside an STL container.
#include <memory>
#include <scoped_allocator>
template <typename Alloc>
struct key_value_pair {
public:
using allocator_type = Alloc;
char, std::char_traits<char>,
typename std::allocator_traits<allocator_type>::template rebind_alloc<
char>>;
explicit key_value_pair(const allocator_type &alloc = allocator_type())
: key(alloc) {}
key_value_pair(const key_value_pair &) = default;
key_value_pair(key_value_pair &&) = default;
key_value_pair(const key_value_pair &other, const allocator_type &alloc)
: key(other.key, alloc) {}
key_value_pair(key_value_pair &&other, const allocator_type &alloc)
: key(std::move(other.key), alloc) {}
key_value_pair &operator=(const key_value_pair &) = default;
key_value_pair &operator=(key_value_pair &&) = default;
key_type key;
int value;
};
template <typename T>
using kv_t = key_value_pair<alloc_t<char>>;
using vec_t =
std::scoped_allocator_adaptor<alloc_t<kv_t>>>;
{
vec->resize(2);
vec->at(0).key = "key0";
vec->at(0).value = 10;
vec->at(1).key = "key1";
vec->at(1).value = 100;
}
{
auto *vec =
manager.find<vec_t>(
"vec").first;
for (const auto &e : *vec) {
std::cout << e.key << " : " << e.value << std::endl;
}
}
return 0;
}
int main()
Definition: jgraph.cpp:24