Use SFINAE to clean up divide by zero protection

This commit is contained in:
Jason Turner
2015-01-12 10:06:42 -07:00
parent 9b3bb493e9
commit 31ef683ced
2 changed files with 7 additions and 21 deletions

View File

@@ -49,32 +49,20 @@ namespace chaiscript
class Boxed_Number
{
private:
#ifndef CHAISCRIPT_NO_PROTECT_DIVIDEBYZERO
template<typename T>
static void check_divide_by_zero(T t)
static void check_divide_by_zero(T t, typename std::enable_if<std::is_integral<T>::value>::type* = 0)
{
#ifdef CHAISCRIPT_MSVC
// MSVC complains that this expression is both constant and the value t is unused
// in the cases of integral expressions. Seems a bit overzealous to me, the parameter
// is only unreferenced because they are eliminating the dead code.
#pragma warning(push)
#pragma warning(disable : 4100 4127)
#endif
if(std::is_integral<T>::value && std::is_arithmetic<T>::value && t==0) {
#ifndef CHAISCRIPT_NO_PROTECT_DIVIDEBYZERO
if (t == 0) {
throw chaiscript::exception::arithmetic_error("divide by zero");
}
#ifdef CHAISCRIPT_MSVC
#pragma warning(pop)
#endif
}
#else
template<typename T>
static void check_divide_by_zero(T)
static void check_divide_by_zero(T, typename std::enable_if<std::is_floating_point<T>::value>::type* = 0)
{
}
#endif
struct boolean
{