我懷疑這可能是原因。 Coliru給出了這樣的錯誤:
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; } \
如果它被定義爲const
而不是constexpr
,這可能是爲什麼它拒絕的代碼。爲了安慰自己這是問題的根源,我們可以重現錯誤與此測試用例:
// 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;
那麼,我們如何解決這個問題?不要使用non-templated version。 Boost提供了templated version,我們可以使用它。
static constexpr double SEC3 = static_cast<double>(45)/180*boost::math::constants::pi<double>();
鐺3.5還實現可變模板與C++ 1Y模式:
template <class T>
static constexpr T SEC3 = static_cast<T>(45)/180*boost::math::constants::pi<T>();
int main()
{
std::cout << SEC3<double>;
}
工程上'的GCC 4.8.2'。你正在使用哪種編譯器? – pmr
哦,你是對的。這令人驚訝......我正在編譯Clang 3.4。有了gcc,它工作正常。 – WestFlame
@pmr顯然這是[gcc bug](http://gcc.gnu.org/PR57979)。 – 2014-02-11 04:52:11