nlohmann/json

`json_fwd.hpp` don't define default template arguments for ordered_map

yyjdelete opened this issue · 7 comments

Description

Unlike json and ordered_json, the default template arguments for ordered_map is not defined in json_fwd.hpp , so when compile failed with template arguments mismatch when use json_fwd.hpp with ordered_map

Reproduction steps

compile the below example code

Expected vs. actual results

the below code example compile failed

Minimal code example

#include <nlohmann/json_fwd.hpp>

void test(nlohmann::ordered_map<std::string, std::string>& val);

Error messages

CLANG
> error: too few template arguments for class template 'ordered_map'

GCC
> error: wrong number of template arguments (2, should be 4)

Compiler and operating system

gcc/clang

Library version

3.11.3

Validation

I understand the issue, but I am not sure if this should be considered a bug, because the goal of the forward header is to make the json types available. I still wonder why

template<class Key, class T, class IgnoredLess, class Allocator>
struct ordered_map;

is not sufficient there. Let me check.

It's sufficient to use ordered_json, but using ordered_map requires specifying all 4 template parameters.

template <class Key, class T, class IgnoredLess = std::less<Key>,
          class Allocator = std::allocator<std::pair<const Key, T>>>
                  struct ordered_map;

This would allow using it with just the normal two.

Thanks @gregmarr. I updated the json_fwd header in #4520.

I get

json/include/nlohmann/ordered_map.hpp:27:51: error: template parameter redefines default argument
   27 | template <class Key, class T, class IgnoredLess = std::less<Key>,
      |                                                   ^
json/include/nlohmann/json_fwd.hpp:68:50: note: previous default template argument defined here
   68 | template<class Key, class T, class IgnoredLess = std::less<Key>,
      |                                                  ^
In file included from json/tests/src/unit-assert_macro.cpp:23:
In file included from json/include/nlohmann/json.hpp:62:
json/include/nlohmann/ordered_map.hpp:28:29: error: template parameter redefines default argument
   28 |           class Allocator = std::allocator<std::pair<const Key, T>>>
      |                             ^
json/include/nlohmann/json_fwd.hpp:69:28: note: previous default template argument defined here
   69 |          class Allocator = std::allocator<std::pair<const Key, T>>>
      |                            ^
2 errors generated.

Looks like you need to move them to json_fwd.hpp (remove them from json.hpp, like the basic_json definition.

Yes, but I'm not sure if this is worth it. So far, ordered_map is a self-contained header that does not "know" of JSON. Making it rely on the forward-declaration header would complicate things (at least to my taste).

@yyjdelete Have you tried including the ordered_map.hpp? Is that sufficient for your needs?

Another option is to add ordered_map_fwd.hpp with the declaration with the default parameters, and include that in ordered_map.hpp and json_fwd.hpp.