Skip to content

alias object_t::key_type in basic_json #1442

@FrancoisChabot

Description

@FrancoisChabot
  • Describe the feature in as much detail as possible.

I propose simply adding using key_type = typename object_t::key_type; to basic_json<>

Considering at() and operator[] already make use of it in the public interface, it doesn't seem unreasonable.

N.B. mapped_type might as well be aliased too for completeness.

Caveat: I understand that find() does not return an iterator to a std::pair<key_type, mapped_type> so basic_json<> can never be a true map-like as it stands, but unlike the key_type issue I illustrate lower down, this is something that can be SFINAE'd around.

  • Include sample usage where appropriate.

This would be useful when duck-typing basic_json<> as a map-like type. Specifically, the fact that std::string_view is not implicitly convertible to std::string is currently making this difficult.

Example:

template<typename MapLike_t>
void do_something(std::string_view key, MapLike_t& obj) {
  using key_type = typename MapLike_t::key_type;

  foo(obj[key_type(key)]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: please discussplease discuss the issue or vote for your favorite optionstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions