From ceea26d144f5d87adc38b818e04a7bc31db21059 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Sun, 5 Jul 2020 12:33:37 -0500 Subject: [PATCH] boost::math::pow is not constexpr #354 (#388) * Made pow constexpr * Added static_assert to test * Changed constexpr to BOOST_CXX14_CONSTEXPR --- .../boost/math/special_functions/math_fwd.hpp | 4 ++-- include/boost/math/special_functions/pow.hpp | 17 ++++++++--------- test/pow_test.cpp | 4 ++++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/boost/math/special_functions/math_fwd.hpp b/include/boost/math/special_functions/math_fwd.hpp index f143c08bc..09cf24c3c 100644 --- a/include/boost/math/special_functions/math_fwd.hpp +++ b/include/boost/math/special_functions/math_fwd.hpp @@ -1019,10 +1019,10 @@ namespace boost // pow: template - typename tools::promote_args::type pow(T base, const Policy& policy); + BOOST_CXX14_CONSTEXPR typename tools::promote_args::type pow(T base, const Policy& policy); template - typename tools::promote_args::type pow(T base); + BOOST_CXX14_CONSTEXPR typename tools::promote_args::type pow(T base); // next: template diff --git a/include/boost/math/special_functions/pow.hpp b/include/boost/math/special_functions/pow.hpp index 9c92116ac..2706fc1b5 100644 --- a/include/boost/math/special_functions/pow.hpp +++ b/include/boost/math/special_functions/pow.hpp @@ -35,7 +35,7 @@ template struct positive_power { template - static T result(T base) + static BOOST_CXX14_CONSTEXPR T result(T base) { T power = positive_power::result(base); return power * power; @@ -46,7 +46,7 @@ template struct positive_power { template - static T result(T base) + static BOOST_CXX14_CONSTEXPR T result(T base) { T power = positive_power::result(base); return base * power * power; @@ -57,7 +57,7 @@ template <> struct positive_power<1, 1> { template - static T result(T base){ return base; } + static BOOST_CXX14_CONSTEXPR T result(T base){ return base; } }; @@ -65,7 +65,7 @@ template struct power_if_positive { template - static T result(T base, const Policy&) + static BOOST_CXX14_CONSTEXPR T result(T base, const Policy&) { return positive_power::result(base); } }; @@ -73,7 +73,7 @@ template struct power_if_positive { template - static T result(T base, const Policy& policy) + static BOOST_CXX14_CONSTEXPR T result(T base, const Policy& policy) { if (base == 0) { @@ -92,7 +92,7 @@ template <> struct power_if_positive<0, true> { template - static T result(T base, const Policy& policy) + static BOOST_CXX14_CONSTEXPR T result(T base, const Policy& policy) { if (base == 0) { @@ -126,15 +126,14 @@ struct select_power_if_positive template -inline typename tools::promote_args::type pow(T base, const Policy& policy) +BOOST_CXX14_CONSTEXPR inline typename tools::promote_args::type pow(T base, const Policy& policy) { typedef typename tools::promote_args::type result_type; return detail::select_power_if_positive::type::result(static_cast(base), policy); } - template -inline typename tools::promote_args::type pow(T base) +BOOST_CXX14_CONSTEXPR inline typename tools::promote_args::type pow(T base) { return pow(base, policies::policy<>()); } #ifdef BOOST_MSVC diff --git a/test/pow_test.cpp b/test/pow_test.cpp index d004f0d9b..eef8e3e6d 100644 --- a/test/pow_test.cpp +++ b/test/pow_test.cpp @@ -194,6 +194,10 @@ BOOST_AUTO_TEST_CASE( test_main ) test_with_big_exponents(); #endif + #ifndef BOOST_NO_CXX14_CONSTEXPR + static_assert(boost::math::pow<8>(2)==256, "Pow is not constexpr"); + #endif + test_return_types(); test_error_policy();