! @brief access specified array element with bounds checking Returns a reference to the element at specified location @a idx, with bounds checking. @param[in] idx index of the element to access @return reference to the element at index @a idx @throw std::domain_error if the JSON value is not an array; example: `"cannot use at() with string"` @throw std::out_of_
| 3149 | @since version 1.0.0 |
| 3150 | */ |
| 3151 | reference at(size_type idx) |
| 3152 | { |
| 3153 | // at only works for arrays |
| 3154 | if (is_array()) |
| 3155 | { |
| 3156 | try |
| 3157 | { |
| 3158 | return m_value.array->at(idx); |
| 3159 | } |
| 3160 | catch (std::out_of_range&) |
| 3161 | { |
| 3162 | // create better exception explanation |
| 3163 | throw std::out_of_range("array index " + std::to_string(idx) + " is out of range"); |
| 3164 | } |
| 3165 | } |
| 3166 | else |
| 3167 | { |
| 3168 | throw std::domain_error("cannot use at() with " + type_name()); |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /*! |
| 3173 | @brief access specified array element with bounds checking |
no test coverage detected