I am writing a JSON class for C++11, see http://github.com/nlohmann/json. My central data structure is a class wrapping the JSON value types (null, array, object, string, bool, number) in a union and offering it in via a nice C++ and STL-like interface. The types for the values can be set via template parameters and default to std::vector
for arrays, std::map
for objects, std::string
for strings, bool
for booleans, int64_t
for integer numbers, and double
for floating-type numbers.
To support the concept of an AllocatorAwareContainer, I also allowed to pass an allocator type which defaults to std::allocator
. As the array, object, and string types are stored with pointers in my union, I use this allocator to allocate memory for these types.
So far, so good (see https://github.com/nlohmann/json/blob/master/src/json.hpp for the complete source code)...
Now my question is whether it makes sense to pass an allocator which is then implicitly used for the types array, object, and string. Alternatively, I could customize the allocator by passing an allocator with the concrete type std::map
, std::vector
, and std::string
. Then, however, I need to be able to specify this specialization, because currently, my class uses the following definition for the templates:
template <
template<typename U, typename V, typename... Args> class ObjectType = std::map,
template<typename U, typename... Args> class ArrayType = std::vector,
class StringType = std::string,
class BooleanType = bool,
class NumberIntegerType = int64_t,
class NumberFloatType = double,
template<typename U> class AllocatorType = std::allocator
>
class basic_json
The problem is, that ArrayType
should be a std::vector<basic_json>
, but the concrete type of basic_json
is yet to be defined. Even worse, ObjectType
should be a mapping from StringType
to basic_json
. The code shown here (and completely found here https://github.com/nlohmann/json/blob/master/src/json.hpp) compiles and is fine, but I have no idea how to allow to specify an allocator per type rather than for the whole class.
Any ideas?