Skip to content

Request: 'emplace_back' #349

Closed
Closed

Description

@jaredgrubb

Out of habit, I tend to use 'emplace_back' instead of 'push_back' and I was surprised basic_json didn't support this. Can we add it?

Activity

  1. nlohmann commented on Nov 2, 2016

    @nlohmann
    Owner

    I have to check whether this makes sense, but you're right that not having it, but having push_back seems odd.

  2. jaredgrubb commented on Nov 2, 2016

    @jaredgrubb
    ContributorAuthor

    I think it's considered part of the various "container" concepts (http://en.cppreference.com/w/cpp/concept/SequenceContainer). Having it missing isn't that big a deal, but would be nice.

  3. self-assigned this
    on Nov 11, 2016
  4. added this to the Release 2.0.8 milestone on Nov 11, 2016
  5. nlohmann commented on Nov 11, 2016

    @nlohmann
    Owner

    Commit 1be73b9 contains a proposal for an emplace_back function (for arrays) and an emplace function (for objects). These functions more or less mimic the behavior of push_back (which is overloaded to add to an array if a basic_json value is passed and to an object if an basic_json::object_t is passed).

    It would be nice to discuss this proposal:

    • Is the implementation as expected?
    • Is it OK to have two functions?
  6. added
    state: please discuss
    on Nov 11, 2016
  7. jaredgrubb commented on Nov 12, 2016

    @jaredgrubb
    ContributorAuthor

    The implementation looks correct. Although on the one hand I think it's odd for the json object to have both methods, it matches the convention that json has object and array methods and it's up to you to call the right ones. So it looks good to me!

  8. nlohmann commented on Nov 12, 2016

    @nlohmann
    Owner

    Thanks. Yes, it is a weird situation to have a combination of a map and a vector, and I am not sure whether users have the mindset "I am using a general container" or rather "I am using an object" / "I am using an array".

    Another aspect: If there was a single emplace function, it could forward the arguments to to the underlying vector or map depending whether the type of the JSON value is and array or object. This would make the automatic translation of null into array or object as it is done in push_back impossible.

  9. nlohmann commented on Nov 24, 2016

    @nlohmann
    Owner

    Any opinions on this?

  10. jaredgrubb commented on Nov 28, 2016

    @jaredgrubb
    ContributorAuthor

    There are times when I wish I had a stronger type that represented what I know it is but I can see the utility of the do-everything type. I dont see a strong reason to change it myself! So The emplace_back would be awesome :)

  11. TurpentineDistillery commented on Nov 28, 2016

    @TurpentineDistillery

    Note that std::map::emplace has a return type that is not void. Was it a conscious decision not to do the same in basic_json-object ?

  12. nlohmann commented on Nov 28, 2016

    @nlohmann
    Owner

    @TurpentineDistillery Good point! I implemented emplace_back before (which is void until C++17) and totally forgot about the return value. I shall fix this.

  13. nlohmann commented on Dec 1, 2016

    @nlohmann
    Owner

    Merged.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions