From 31ef683cedd35fb0ff10094f7c7649b97832b86d Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 12 Jan 2015 10:06:42 -0700 Subject: [PATCH] Use SFINAE to clean up divide by zero protection --- .../chaiscript/dispatchkit/boxed_number.hpp | 22 +++++-------------- unittests/future.chai | 6 ++--- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index c00911c..a0ad6aa 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -49,32 +49,20 @@ namespace chaiscript class Boxed_Number { private: -#ifndef CHAISCRIPT_NO_PROTECT_DIVIDEBYZERO template - static void check_divide_by_zero(T t) + static void check_divide_by_zero(T t, typename std::enable_if::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::value && std::is_arithmetic::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 - static void check_divide_by_zero(T) + static void check_divide_by_zero(T, typename std::enable_if::value>::type* = 0) { } -#endif struct boolean { diff --git a/unittests/future.chai b/unittests/future.chai index afb9afe..750c27d 100644 --- a/unittests/future.chai +++ b/unittests/future.chai @@ -1,6 +1,6 @@ var func = fun(){ var ret = 0; - for (var i = 0; i < 1000000; ++i) { + for (var i = 0; i < 50000; ++i) { ret += i; } return ret; @@ -9,9 +9,7 @@ var func = fun(){ var fut1 := async(func); var fut2 := async(func); -var fut3 := async(func); -var fut4 := async(func); // simply executing without crashing is good enough for this test -print(" ${fut1.get()} ${fut2.get()} ${fut3.get()} ${fut4.get()}") +print(" ${fut1.get()} ${fut2.get()} ")