Skip to content

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

Closed
@YarikTH

Description

@YarikTH

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

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions