-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for unordered_map as object_t #2932
Comments
Unfortunately, using |
The first template parameter to template<typename U, typename V, typename... Args> class ObjectType = std::map Originally, we assumed This would allow to pass both template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
> class map; and template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map; The problem comes later, when inside using object_t = ObjectType<
StringType,
basic_json,
object_comparator_t,
AllocatorType<std::pair<const StringType, basic_json>>
>; This implicitly assumes that the third template parameter is a comparator and the forth parameter is an allocator. This, however, is only true for Note in #if defined(JSON_HAS_CPP_14)
using object_comparator_t = std::less<>;
#else
using object_comparator_t = std::less<StringType>;
#endif So in order to allow using ujson = nlohmann::basic_json<std::unordered_map>;
ujson j; |
Yep, that's what I gathered as well. Now I had an idea: If we want "defaults only" and just use defaults for the hash function, equality comparator and allocator, I thought one could do: template<class K, class V, class...>
using default_unordered_map = std::unordered_map<K, V>;
using ujson = nlohmann::basic_json<default_unordered_map>;
ujson j; This gets rid of a number of errors, but complains that |
I'm currently trying this: using object_t = typename std::conditional<
true,
std::map<StringType,
basic_json,
object_comparator_t,
AllocatorType<std::pair<const StringType,
basic_json>>>,
std::unordered_map<StringType,
basic_json,
std::hash<StringType>,
std::equal_to<StringType>,
AllocatorType<std::pair<const StringType,
basic_json>>>
>::type; The upper case is the current default (using |
I made some progress (see #2956), but I guess I now hit a blocker: Any ideas how to fix thisß |
No, not really. Other people have the same problem and there doesn't seem to be a proper solution except using an pointer indirection. |
Then I guess all i can do is adjust the documentation that |
Too bad :-( Maybe we can come up with our own container solution that somehow wraps |
Hi, do you have a branch where you were working on this? |
Yes, see #2956. |
Is
std::unordered_map
supposed to be supported by now? The documentation mentions this (https://json.nlohmann.me/api/basic_json/object_t/#template-parameters), but it still fails to compile.I know there've been passt issues tagged with
wontfix
. Just asking becausestd::map
actually seems to be a bottleneck in our use-case and the docs still mentionstd::unordered_map
.The text was updated successfully, but these errors were encountered: