TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ emplace_back()

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<class... Args>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::emplace_back ( Args &&...  args)
inline

add an object to an array

Creates a JSON value from the passed parameters args to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending the value created from args.

Parameters
[in]argsarguments to forward to a constructor of basic_json
Template Parameters
Argscompatible types to create a basic_json object
Returns
reference to the inserted element
Exceptions
type_error.311when called on a type other than JSON array or null; example: "cannot use emplace_back() with number"

@complexity Amortized constant.

@liveexample{The example shows how push_back() can be used to add elements to a JSON array. Note how the null value was silently converted to a JSON array.,emplace_back}

Since
version 2.0.8, returns reference since 3.7.0

Definition at line 23342 of file json.hpp.

23343 {
23344 // emplace_back only works for null objects or arrays
23345 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23346 {
23347 JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this));
23348 }
23349
23350 // transform null object into an array
23351 if (is_null())
23352 {
23353 m_type = value_t::array;
23356 }
23357
23358 // add element to array (perfect forwarding)
23359 const auto old_capacity = m_value.array->capacity();
23360 m_value.array->emplace_back(std::forward<Args>(args)...);
23361 return set_parent(m_value.array->back(), old_capacity);
23362 }
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
json_value m_value
the value of the current element
Definition: json.hpp:24928
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:20432
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:20251
@ array
array (ordered collection of values)