Skip to content
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

Missing "void insert( InputIt first, InputIt last );" overload in nlohmann::ordered_map #2490

Closed
1 of 3 tasks
YarikTH opened this issue Dec 2, 2020 · 3 comments
Closed
1 of 3 tasks
Assignees
Labels
kind: bug release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@YarikTH
Copy link
Contributor

YarikTH commented Dec 2, 2020

What is the issue you have?

I tried to explicitly instantiate ordered_json to improve my compilation time. Unfortunately, it doesn't work because nlohmann::ordered_map is missing ranged insert overload that is requested from
void basic_json::insert(const_iterator first, const_iterator last)

See ranged std::map::insert (7)
https://en.cppreference.com/w/cpp/container/map/insert

Can you provide a small but working code example?

#include "nlohmann/json.hpp"

static_assert(NLOHMANN_JSON_VERSION_MAJOR == 3, "");
static_assert(NLOHMANN_JSON_VERSION_MINOR == 9, "");
static_assert(NLOHMANN_JSON_VERSION_PATCH == 1, "");

// hpp
namespace nlohmann
{
    // explicit instantiation of ordered_json
    extern template class basic_json<nlohmann::ordered_map>;
}

// cpp
namespace nlohmann
{
    // explicit instantiation of ordered_json
    template class basic_json<nlohmann::ordered_map>; //<<< here we have a problem
}

https://godbolt.org/z/8o5cbf

Possible compiler output:

In file included from <source>:1:
/opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:22318:25: error: no matching member function for call to 'insert'
        m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
        ~~~~~~~~~~~~~~~~^~~~~~
<source>:18:20: note: in instantiation of member function 'nlohmann::basic_json<nlohmann::ordered_map, std::vector, std::__cxx11::basic_string<char>, bool, long, unsigned long, double, std::allocator, adl_serializer, std::vector<unsigned char, std::allocator<unsigned char>>>::insert' requested here
    template class basic_json<nlohmann::ordered_map>; //<<< here we have a problem
                   ^
/opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:16620:31: note: candidate function not viable: requires single argument 'value', but 2 arguments were provided
    std::pair<iterator, bool> insert( value_type&& value )
                              ^
/opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:16625:31: note: candidate function not viable: requires single argument 'value', but 2 arguments were provided
    std::pair<iterator, bool> insert( const value_type& value )
                              ^

My current dirty fix is:

template <class Key, class T, class IgnoredLess = std::less<Key>,
          class Allocator = std::allocator<std::pair<const Key, T>>>
                  struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
{
...
    template<typename InputIt>
    using require_input_iter = typename
      std::enable_if<std::is_convertible<typename
        std::iterator_traits<InputIt>::iterator_category,
                   std::input_iterator_tag>::value>::type;

    template<typename InputIt, typename = require_input_iter<InputIt>>
    void insert( InputIt first, InputIt last )
    {
        for (auto it = first; it != last; ++it)
        {
            insert( *it );
        }
    }

It's not optimized and I'm not tested if it works correctly, or just compiles.

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch
@nlohmann
Copy link
Owner

nlohmann commented Dec 2, 2020

Thanks for reporting! I'll have a look, but this will take time. In the meantime, PRs welcome!

@YarikTH
Copy link
Contributor Author

YarikTH commented Dec 2, 2020

I'll try to make a PR in the near future

@nlohmann
Copy link
Owner

Closed by merging #2512.

@nlohmann nlohmann added release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: waiting for PR labels Dec 11, 2020
@nlohmann nlohmann self-assigned this Dec 11, 2020
@nlohmann nlohmann added this to the Release 3.9.2 milestone Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants