@@ -71,9 +71,7 @@ struct json
7171 }
7272 }
7373
74- constexpr const json *operator ->() const {
75- return &(*(*this ));
76- }
74+ constexpr const json *operator ->() const { return &(*(*this )); }
7775
7876 constexpr std::size_t index () const { return index_; }
7977
@@ -177,13 +175,10 @@ struct json
177175 }
178176 }
179177
180- constexpr iterator find (const std::string_view key) const
178+ constexpr iterator find (const std::string_view key) const
181179 {
182- for (auto itr = begin (); itr != end (); ++itr)
183- {
184- if (itr.key () == key) {
185- return itr;
186- }
180+ for (auto itr = begin (); itr != end (); ++itr) {
181+ if (itr.key () == key) { return itr; }
187182 }
188183
189184 return end ();
@@ -193,13 +188,26 @@ struct json
193188 {
194189 const auto &children = object_data ();
195190
196- // find_if is not constexpr yet in C++17
197- for (const auto &value : children) {
198- // cppcheck-suppress useStlAlgorithm
199- if (value.first == key) { return value.second ; }
200- }
191+ // find_if is not constexpr in C++17, so we rolled our own,
192+ // and this helps us work around bugs in older versions of GCC
193+ // and constexpr
194+ const auto find = [&]() {
195+ auto itr = children.begin ();
196+
197+ for (; itr != children.end (); ++itr) {
198+ if (itr->first == key) { return itr; }
199+ }
200+
201+ return itr;
202+ };
203+
204+ const auto obj = find ();
201205
202- throw std::runtime_error (" Key not found" );
206+ if (obj != children.end ()) {
207+ return obj->second ;
208+ } else {
209+ throw std::runtime_error (" Key not found" );
210+ }
203211 }
204212
205213 constexpr const array_t &array_data () const
0 commit comments