diff --git a/toml/value.hpp b/toml/value.hpp index b7066c6..7f6f42b 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -744,6 +744,16 @@ void change_region(value& v, Region&& reg) return; } +template +[[noreturn]] inline void throw_bad_cast(value_t actual, const ::toml::value& v) +{ + throw type_error(detail::format_underline(concat_to_string( + "[error] toml::value bad_cast to ", Expected), { + {std::addressof(get_region(v)), + concat_to_string("the actual type is ", actual)} + })); +} + template struct switch_cast; template<> @@ -823,11 +833,7 @@ typename detail::toml_default_type::type& value::cast() & { if(T != this->type_) { - throw type_error(detail::format_underline(concat_to_string( - "[error] toml::value bad_cast to ", T), { - {this->region_info_.get(), - concat_to_string("the actual type is ", this->type_)} - })); + detail::throw_bad_cast(this->type_, *this); } return detail::switch_cast::invoke(*this); } @@ -836,11 +842,7 @@ typename detail::toml_default_type::type const& value::cast() const& { if(T != this->type_) { - throw type_error(detail::format_underline(concat_to_string( - "[error] toml::value bad_cast to ", T), { - {this->region_info_.get(), - concat_to_string("the actual type is ", this->type_)} - })); + detail::throw_bad_cast(this->type_, *this); } return detail::switch_cast::invoke(*this); } @@ -849,11 +851,7 @@ typename detail::toml_default_type::type&& value::cast() && { if(T != this->type_) { - throw type_error(detail::format_underline(concat_to_string( - "[error] toml::value bad_cast to ", T), { - {this->region_info_.get(), - concat_to_string("the actual type is ", this->type_)} - })); + detail::throw_bad_cast(this->type_, *this); } return detail::switch_cast::invoke(std::move(*this)); }