| 5692 | */ |
| 5693 | template<class... Args> |
| 5694 | std::pair<iterator, bool> emplace(Args&& ... args) |
| 5695 | { |
| 5696 | // emplace only works for null objects or arrays |
| 5697 | if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) |
| 5698 | { |
| 5699 | JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this)); |
| 5700 | } |
| 5701 | |
| 5702 | // transform null object into an object |
| 5703 | if (is_null()) |
| 5704 | { |
| 5705 | m_type = value_t::object; |
| 5706 | m_value = value_t::object; |
| 5707 | assert_invariant(); |
| 5708 | } |
| 5709 | |
| 5710 | // add element to array (perfect forwarding) |
| 5711 | auto res = m_value.object->emplace(std::forward<Args>(args)...); |
| 5712 | set_parent(res.first->second); |
| 5713 | |
| 5714 | // create result iterator and set iterator to the result of emplace |
| 5715 | auto it = begin(); |
| 5716 | it.m_it.object_iterator = res.first; |
| 5717 | |
| 5718 | // return pair of iterator and boolean |
| 5719 | return {it, res.second}; |
| 5720 | } |
| 5721 | |
| 5722 | /// Helper for insertion of an iterator |
| 5723 | /// @note: This uses std::distance to support GCC 4.8, |