@@ -366,26 +366,36 @@ template<typename CharType> struct basic_json
366
366
367
367
template <typename Type>[[nodiscard]] constexpr Type get () const
368
368
{
369
+ bool error = false ;
369
370
if constexpr (std::is_same_v<Type, std::uint64_t > || std::is_same_v<Type, std::int64_t >) {
370
371
if (const auto *uint_value = data.get_if_uinteger (); uint_value != nullptr ) {
371
372
return Type (*uint_value);
372
373
} else if (const auto *value = data.get_if_integer (); value != nullptr ) {
373
374
return Type (*value);
374
375
}
375
- throw std::runtime_error ( " Incorrect type for get(), integer requested " ) ;
376
+ error = true ;
376
377
} else if constexpr (std::is_same_v<Type, double >) {
377
378
if (const auto *value = data.get_if_floating_point (); value != nullptr ) { return *value; }
378
- throw std::runtime_error ( " Incorrect type for get(), double requested " ) ;
379
+ error = true ;
379
380
} else if constexpr (std::is_same_v<Type,
380
381
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
381
382
if (const auto *value = data.get_if_string (); value != nullptr ) { return *value; }
382
- throw std::runtime_error ( " Incorrect type for get(), string requested " ) ;
383
+ error = true ;
383
384
} else if constexpr (std::is_same_v<Type, bool >) {
384
385
if (const auto *value = data.get_if_boolean (); value != nullptr ) { return *value; }
385
- throw std::runtime_error ( " Incorrect type for get(), boolean requested " ) ;
386
+ error = true ;
386
387
} else {
387
388
throw std::runtime_error (" Unexpected type for get()" );
388
389
}
390
+
391
+ if (error) {
392
+ // we have this boolean only because of a broken gcc implementation
393
+ // that incorrect says this is not a constexpr function
394
+ throw std::runtime_error (" Type mismatch in get()" );
395
+ } else {
396
+ // this code is terrible and it makes me sad
397
+ return Type{};
398
+ }
389
399
}
390
400
391
401
[[nodiscard]] constexpr bool is_object () const noexcept { return data.selected == data_t ::selected_type::object; }
0 commit comments