using boost math constants in constexpr

早过忘川 提交于 2019-12-06 02:31:11

I suspect this may be the reason. Coliru gives this error:

clang++ -std=c++1y -O2 -Wall -pedantic -pthread main.cpp && ./a.out

/usr/local/include/boost/math/constants/constants.hpp:248:52: note: expanded from macro 'BOOST_DEFINE_MATH_CONSTANT'

   namespace double_constants{ static const double name = x; } \

If it's defined as const and not constexpr, that may be why it's rejecting the code. To reassure ourselves that is the source of the issue, we can reproduce the error with this testcase:

// This code fails
#include <boost/math/constants/constants.hpp>
namespace double_constants{ static const double name = 25; }

static constexpr double SEC3 = static_cast<double>(45)/180*double_constants::name;

So how do we fix this? Don't use the non-templated version. Boost provides a templated version that we can use instead.

static constexpr double SEC3 = static_cast<double>(45)/180*boost::math::constants::pi<double>();

clang 3.5 also implements variable templates with C++1y mode:

template <class T>
static constexpr T SEC3 = static_cast<T>(45)/180*boost::math::constants::pi<T>();

int main()
{
    std::cout << SEC3<double>;
}

I saw this post when I was looking for the cleanest way to define pi as a constexpr double using the boost library. The following code snippet works well for me in Visual Studio 2017 using boost 1_66_0:

#include <boost/math/constants/constants.hpp>
constexpr double pi = boost::math::constants::pi<double>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!