Skip to content
Snippets Groups Projects
Commit 57309b8d authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[numeric_cast] add numeric_cast<target>(source)

parent 6356a2c1
No related branches found
No related tags found
No related merge requests found
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment