diff --git a/dune/xt/common/numeric_cast.hh b/dune/xt/common/numeric_cast.hh new file mode 100644 index 0000000000000000000000000000000000000000..cb68868c44e799c9564e8c89b70065b5032328a0 --- /dev/null +++ b/dune/xt/common/numeric_cast.hh @@ -0,0 +1,39 @@ +#ifndef DUNE_XT_COMMON_NUMERIC_CAST_HH +#define DUNE_XT_COMMON_NUMERIC_CAST_HH + +#include <boost/numeric/conversion/cast.hpp> + +#include "exceptions.hh" +#include "type_traits.hh" + +namespace Dune { +namespace XT { +namespace Common { + + +/** + * \brief Calls boost::numeric_cast and gives some information on failure. + */ +template <class T, class S> +T numeric_cast(S source) +{ + T target; + try { + target = boost::numeric_cast<size_t>(source); + } catch (boost::bad_numeric_cast& ee) { + DUNE_THROW(XT::Common::Exceptions::external_error, + " The following error occured in boost while converting '" << source << "' to '" + << XT::Common::Typename<size_t>::value() + << ":\n\n" + << ee.what()); + } + return target; +} // ... numeric_cast(...) + + +} // namespace Common +} // namespace XT +} // namespace Dune + + +#endif // DUNE_XT_COMMON_NUMERIC_CAST_HH