@@ -366,26 +366,36 @@ template<typename CharType> struct basic_json
366366
367367 template <typename Type>[[nodiscard]] constexpr Type get () const
368368 {
369+ bool error = false ;
369370 if constexpr (std::is_same_v<Type, std::uint64_t > || std::is_same_v<Type, std::int64_t >) {
370371 if (const auto *uint_value = data.get_if_uinteger (); uint_value != nullptr ) {
371372 return Type (*uint_value);
372373 } else if (const auto *value = data.get_if_integer (); value != nullptr ) {
373374 return Type (*value);
374375 }
375- throw std::runtime_error ( " Incorrect type for get(), integer requested " ) ;
376+ error = true ;
376377 } else if constexpr (std::is_same_v<Type, double >) {
377378 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 ;
379380 } else if constexpr (std::is_same_v<Type,
380381 std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
381382 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 ;
383384 } else if constexpr (std::is_same_v<Type, bool >) {
384385 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 ;
386387 } else {
387388 throw std::runtime_error (" Unexpected type for get()" );
388389 }
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+ }
389399 }
390400
391401 [[nodiscard]] constexpr bool is_object () const noexcept { return data.selected == data_t ::selected_type::object; }
0 commit comments