I have written the following code:
#include<array>
#include<type_traits>
namespace math{
namespace detail{
template<std::size_t... Is> struct seq{};
template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...>{};
template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...>{};
template<class T,std::size_t N>
struct sin_coeffs{
using array_type = std::array<T,N>;
constexpr static inline T coeff(std::size_t n){
return power(-1, n-1) * inverse((T)factorial((2 * n)-1));
}
template<std::size_t...NS>
constexpr static array_type _coeffs(seq<NS...>){
return {{coeff(NS)...}};
}
constexpr static array_type coeffs=_coeffs(gen_seq<N>{});
};
}
template<class T,std::size_t N = max_factorial, class dcy = std::decay_t<T>>
constexpr std::enable_if_t<std::is_floating_point<dcy>::value,dcy> sin(T x) noexcept{
constexpr std::array<dcy,N>& coeffs = detail::sin_coeffs<dcy,N>::coeffs;
const dcy x_2 = x*x;
dcy pow = x;
dcy result = 0;
for(std::size_t i=0;i<N;++i){
result += coeffs[i] * pow;
pow*=x_2;
}
return result;
}
}
example main:
int main()
{
constexpr double d = math::sin(0.0);
}
The code is designed to be a constexpr sin function that uses a constexpr array that holds the coefficients to do the needed calculations.
All of the functions not listed are present in a separate header and compile with no problem.
I am trying to use the "indicies" trick to fill the array using a constexpr function based off this answer to another question.
I am compiling with GCC 5.3.1 with the flags
--std=c++1z -pthread -g -O3 -MMD -MP -Wall -pedantic
The compiler emits no errors to my code but stalls when compiling.
I have let the compilation run for a few minutes but it does not do anything.
I have tested all of the functions used in the code and they all compile just fine independently of this section.
int main()
{
math::detail::sin_coeffs<double,20>::coeffs[0];
}
This code snippet also reproduces the issue which leads me to believe that it is not related to the sin function itself but the sin_coeffs struct.
EDIT: Here are the other functions as requested:
#include <type_traits>
namespace math{
template<class T,class dcy = std::decay_t<T>>
constexpr inline std::enable_if_t<std::is_floating_point<T>::value,dcy> inverse(T value){
return (value == 0) ? 0.0 : 1.0 / value;
}
template <class T>
constexpr inline std::decay_t<T> sign(T value) {
return value < 0 ? -1 : 1;
}
template <typename T>
constexpr inline std::decay_t<T> abs(T value) {
return value * sign(value);
}
template<class T>
constexpr inline std::decay_t<T> power(T const& base, std::size_t const& pow){
if(pow==0){return 1;}
else if(pow == 1){return base;}
else{
T result = base;
for(std::size_t i=1;i<pow;++i){
result*=base;
}
return result;
}
}
constexpr std::intmax_t factorial(std::intmax_t const& n){
if(n==0){return 1;}
std::intmax_t result = n;
for(std::intmax_t i=n-1;i>0;--i){
result *=i;
}
return result;
}
constexpr static std::size_t max_factorial = 20;//replace with calculated version later
}