You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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!
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.
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 :)
@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.
Activity
nlohmann commentedon Nov 2, 2016
I have to check whether this makes sense, but you're right that not having it, but having
push_backseems odd.jaredgrubb commentedon Nov 2, 2016
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.
proposal for emplace/emplace_back (#349)
nlohmann commentedon Nov 11, 2016
Commit 1be73b9 contains a proposal for an
emplace_backfunction (for arrays) and anemplacefunction (for objects). These functions more or less mimic the behavior ofpush_back(which is overloaded to add to an array if abasic_jsonvalue is passed and to an object if anbasic_json::object_tis passed).It would be nice to discuss this proposal:
jaredgrubb commentedon Nov 12, 2016
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!
nlohmann commentedon Nov 12, 2016
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
emplacefunction, 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 inpush_backimpossible.nlohmann commentedon Nov 24, 2016
Any opinions on this?
jaredgrubb commentedon Nov 28, 2016
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 :)
TurpentineDistillery commentedon Nov 28, 2016
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 ?
nlohmann commentedon Nov 28, 2016
@TurpentineDistillery Good point! I implemented
emplace_backbefore (which is void until C++17) and totally forgot about the return value. I shall fix this.✨ added return value for emplace (#349)
nlohmann commentedon Dec 1, 2016
Merged.