! @brief access specified array element Returns a reference to the element at specified location @a idx. @note If @a idx is beyond the range of the array (i.e., `idx >= size()`), then the array is silently filled up with `null` values to make `idx` a valid reference to the last stored element. @param[in] idx index of the element to access @return reference to the e
| 15873 | @since version 1.0.0 |
| 15874 | */ |
| 15875 | reference operator[](size_type idx) |
| 15876 | { |
| 15877 | // implicitly convert null value to an empty array |
| 15878 | if (is_null()) |
| 15879 | { |
| 15880 | m_type = value_t::array; |
| 15881 | m_value.array = create<array_t>(); |
| 15882 | assert_invariant(); |
| 15883 | } |
| 15884 | |
| 15885 | // operator[] only works for arrays |
| 15886 | if (JSON_LIKELY(is_array())) |
| 15887 | { |
| 15888 | // fill up array with null values if given idx is outside range |
| 15889 | if (idx >= m_value.array->size()) |
| 15890 | { |
| 15891 | m_value.array->insert(m_value.array->end(), |
| 15892 | idx - m_value.array->size() + 1, |
| 15893 | basic_json()); |
| 15894 | } |
| 15895 | |
| 15896 | return m_value.array->operator[](idx); |
| 15897 | } |
| 15898 | |
| 15899 | JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); |
| 15900 | } |
| 15901 | |
| 15902 | /*! |
| 15903 | @brief access specified array element |
nothing calls this directly
no test coverage detected