danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
699 stars 158 forks source link

Assert triggers on key_value when using polymorphic allocator #424

Closed royjacobson closed 1 year ago

royjacobson commented 1 year ago

Hi!

the following code -

#include "jsoncons/json.hpp"
#include <memory>
#include <memory_resource>

struct pmr_sorted_policy : public jsoncons::sorted_policy {
  template <class T, class Allocator> using vector = std::pmr::vector<T>;

  template <class KeyT, class Json>
  using sorted_json_object = jsoncons::sorted_json_object<KeyT, Json, vector>;

  template <class Json> using array = jsoncons::json_array<Json, vector>;

  template <class CharT, class CharTraits, class Allocator>
  using string = std::pmr::basic_string<CharT>;
};

using custom_json = jsoncons::basic_json<char, pmr_sorted_policy,
                                         std::pmr::polymorphic_allocator<char>>;

static custom_json j;

triggers the following assert in libstdc++:

      static_assert(__or_<
      is_constructible<_Tp, allocator_arg_t, const _Alloc&, _Args...>,
      is_constructible<_Tp, _Args..., const _Alloc&>>::value,
      "construction with an allocator must be possible"
      " if uses_allocator is true");

It seems to complain about jsoncons::key_value that has a member allocator_type but has no related allocator constructors. Should the relevant constructors be added to jsoncons::key_value?

danielaparker commented 1 year ago

Thanks for reporting this. The issues it raises are related to #419.

The first issue is that key_value shouldn't have an allocator_type member. I've removed it on master.

I need to think about the next issue.

danielaparker commented 1 year ago

We've added support for std::pmr::polymorphic_allocator and std::scoped_allocator_adaptor in branch PolymorphicAllocator.

We've defined aliases and alias templates for basic_json using polymorphic allocators in the jsoncons::pmr namespace, see basic_json.

For examples of polymorphic_allocator, see polymorphic_allocator_tests.cpp.

For examples of scoped_allocator_adaptor, see scoped_allocator_adaptor_tests.cpp.

Support for regular (non-propagating) stateful allocators has been dropped, non-propagating stateful allocators must now be wrapped with a std::scoped_allocator_adaptor. We've done that for our example with the boost interprocess allocator, see the example. Attempting to instantiate a basic_json with a regular stateful allocator not wrapped in std::scoped_allocator_adaptor will produce a compile time error.

Non-stateful allocators are supported as before.

danielaparker commented 1 year ago

This is currently on master.