Skip to content

In basic_json::basic_json(const CompatibleArrayType& val), the requirement of CompatibleArrayType is not strict enough. #174

Closed

Description

@jasper412

This is a question Ralated to #167 :
In basic_json::basic_json(const CompatibleArrayType& val), the requirement of CompatibleArrayType is not strict enough.

template <class CompatibleArrayType, typename
              std::enable_if<
                  not std::is_same<CompatibleArrayType, typename basic_json_t::iterator>::value and
                  not std::is_same<CompatibleArrayType, typename basic_json_t::const_iterator>::value and
                  not std::is_same<CompatibleArrayType, typename basic_json_t::reverse_iterator>::value and
                  not std::is_same<CompatibleArrayType, typename basic_json_t::const_reverse_iterator>::value and
                  not std::is_same<CompatibleArrayType, typename array_t::iterator>::value and
                  not std::is_same<CompatibleArrayType, typename array_t::const_iterator>::value and
                  std::is_constructible<basic_json, typename CompatibleArrayType::value_type>::value, int>::type
              = 0>
    basic_json(const CompatibleArrayType& val)
        : m_type(value_t::array)
    {
        using std::begin;
        using std::end;
        m_value.array = create<array_t>(begin(val), end(val));
    }

Template constructor of basic_json_t above can receive any type that has a type member value_type which can be used as the only parameter in one of basic_json_t's constructors and regard that type as a CompatibleArrayType.
I think the scope of the target types is too much wide, though it excludes some of the types already meet the requirement but are not CompatibleArrayType, still not fine with 3rd party types.

Activity

  1. nlohmann commented on Jan 10, 2016

    @nlohmann
    Owner

    The code was inspired by some similar constructor from dropbox/json11. In fact, I only added constraints to make the code compile in my setting.

    @mysyljr, do you have an example where the code actually breaks?

  2. added
    state: please discuss
    on Jan 10, 2016
  3. jasper412 commented on Jan 11, 2016

    @jasper412
    Author

    Simply define a type whose value_type is basic_json< xxx > will break the functionality, in which case a wrong conversion will be defined and unless you define a operator<< (or anything basic_json has) for that type, otherwise calling a operator<< will instantialize the conversion constructor ending with a compiling error.

  4. nlohmann commented on Jan 20, 2016

    @nlohmann
    Owner

    I do not really see the need to change the library's code nor how a change would look like...

  5. nlohmann commented on Jan 26, 2016

    @nlohmann
    Owner

    @mysyljr Sorry, I really need an actual example to proceed here.

  6. removed
    state: please discuss
    on Nov 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions