Skip to content

Commit 91b12ab

Browse files
committed
Port to GCC 9
1 parent 9d87ad3 commit 91b12ab

3 files changed

Lines changed: 29 additions & 57 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ dynamic_project_options(
7272
# GCC_WARNINGS # Override the defaults for the GCC warnings
7373
)
7474

75+
76+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9")
77+
target_link_libraries(project_options INTERFACE stdc++fs)
78+
endif()
79+
80+
7581
target_compile_features(project_options INTERFACE cxx_std_${USER_CXX_STANDARD})
7682

7783
# Adding the src:

build_examples.sh

Lines changed: 0 additions & 42 deletions
This file was deleted.

include/json2cpp/constexpr_json.hpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)