* Accept lvalues in ordered_map::emplace's value parameter
ordered_map::emplace(key, value) took the mapped value only by T&&, an
rvalue reference rather than a forwarding reference, so
ordered_json::emplace("a", value) failed to compile whenever value was
an lvalue or a const lvalue, even though the same call compiles for
json (whose object_t is std::map, with a variadic emplace). Turn the
value parameter into a separately-deduced forwarding reference,
constrained with std::is_constructible so the overloads still only
accept something convertible to the mapped type. std::map-compatible
semantics are unchanged: emplace still does nothing if the key already
exists.
Open PR #5609 also touches ordered_map.hpp (moving values on vector
growth); this change only touches the two emplace() overloads and
should not conflict.
Fixes #5673.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* Avoid astyle's padding in ordered_map::emplace's template headers
Use detail::conjunction instead of && and drop the redundant V&& in detail::is_constructible, so astyle keeps the usual template formatting. Addresses review comment by @gregmarr.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2.4 KiB
nlohmann::basic_json::emplace
template<class... Args>
std::pair<iterator, bool> emplace(Args&& ... args);
Inserts a new element into a JSON object constructed in-place with the given args if there is no element with the key
in the container. If the function is called on a JSON null value, an empty object is created before appending the value
created from args.
Template parameters
Args- compatible types to create a
basic_jsonobject
Iterator invalidation
For ordered_json, adding a value to an object can yield a reallocation, in which case all
iterators (including the end() iterator) and all references to the elements are invalidated.
Parameters
args(in)- arguments to forward to a constructor of
basic_json
Return value
a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and
a #!cpp bool denoting whether the insertion took place.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value. As an exception, a #!json null
value is converted to an empty object before the element is added and keeps that type if adding the element throws.
Exceptions
Throws type_error.311 when called on a type other than JSON
object or #!json null; example: "cannot use emplace() with number"
Complexity
Logarithmic in the size of the container, O(log(size())).
Examples
??? example
The example shows how `emplace()` can be used to add elements to a JSON object. Note how the `#!json null` value was
silently converted to a JSON object. Further note how no value is added if there was already one value stored with
the same key.
```cpp
--8<-- "examples/emplace.cpp"
```
Output:
```json
--8<-- "examples/emplace.output"
```
See also
- emplace_back add a value to an array
- insert add values to an array/object
- Modifying values - the article on modifying values
Version history
- Since version 2.0.8.
- Fixed in version 3.13.0: for
ordered_json, the value could previously only be passed as an rvalue; it can now also be passed as an lvalue or a#!cpp constlvalue, matching the behavior ofjson.