Skip to content

Commit 3436a66

Browse files
committed
Account for broken older gcc/constexpr support
1 parent b34d892 commit 3436a66

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

include/json2cpp/json2cpp.hpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,7 @@ template<typename CharType> struct basic_json
287287

288288
[[nodiscard]] constexpr iterator cend() const noexcept { return end(); }
289289

290-
[[nodiscard]] constexpr std::size_t size() const noexcept
291-
{
292-
return size_;
293-
}
290+
[[nodiscard]] constexpr std::size_t size() const noexcept { return size_; }
294291

295292
[[nodiscard]] static constexpr std::size_t size(const basic_json &obj) noexcept
296293
{
@@ -367,26 +364,28 @@ template<typename CharType> struct basic_json
367364
constexpr static basic_json object() { return basic_json{ data_t{ basic_object_t<CharType>{} } }; }
368365
constexpr static basic_json array() { return basic_json{ data_t{ basic_array_t<CharType>{} } }; }
369366

370-
template<typename Type> [[nodiscard]] constexpr Type get() const
367+
template<typename Type>[[nodiscard]] constexpr Type get() const
371368
{
372369
if constexpr (std::is_same_v<Type, std::uint64_t> || std::is_same_v<Type, std::int64_t>) {
373370
if (const auto *uint_value = data.get_if_uinteger(); uint_value != nullptr) {
374371
return Type(*uint_value);
375372
} else if (const auto *value = data.get_if_integer(); value != nullptr) {
376373
return Type(*value);
377374
}
375+
throw std::runtime_error("Incorrect type for get(), integer requested");
378376
} else if constexpr (std::is_same_v<Type, double>) {
379377
if (const auto *value = data.get_if_floating_point(); value != nullptr) { return *value; }
378+
throw std::runtime_error("Incorrect type for get(), double requested");
380379
} else if constexpr (std::is_same_v<Type,
381380
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
382381
if (const auto *value = data.get_if_string(); value != nullptr) { return *value; }
382+
throw std::runtime_error("Incorrect type for get(), string requested");
383383
} else if constexpr (std::is_same_v<Type, bool>) {
384384
if (const auto *value = data.get_if_boolean(); value != nullptr) { return *value; }
385+
throw std::runtime_error("Incorrect type for get(), boolean requested");
385386
} else {
386387
throw std::runtime_error("Unexpected type for get()");
387388
}
388-
389-
throw std::runtime_error("Incorrect type for get()");
390389
}
391390

392391
[[nodiscard]] constexpr bool is_object() const noexcept { return data.selected == data_t::selected_type::object; }
@@ -421,13 +420,13 @@ template<typename CharType> struct basic_json
421420

422421

423422
data_t data;
424-
std::size_t size_{size(*this)};
423+
std::size_t size_{ size(*this) };
425424
};
426425

427426
using json = basic_json<char>;
428427
using object_t = basic_object_t<char>;
429428
using value_pair_t = basic_value_pair_t<char>;
430429
using array_t = basic_array_t<char>;
431-
}// namespace constexpr_json
430+
}// namespace json2cpp
432431

433432
#endif

0 commit comments

Comments
 (0)