I'm trying to build a simple sine
function using taylor series expansion that can be evaluated at compile time using C++14 constexpr
. My code is compiling, but the compiler doesn't generate a constant.
sine
is defined as follows:
template <int P, typename T = double> constexpr T sine(T x) {
T result = x;
for (int i = 1; i < P; ++i)
result += power<T>(-1, i) * power<T>(x, 1 + 2 * i) / factorial<T>(1 + 2 * i);
return result;
}
I can provide code for power
and factorial
if needed. They are trivial and also constexpr
.
I'm calling sine
from within a loop like this:
template <int N> void test(double *out) {
for (int i = 0; i < N; ++i) {
out[i] = sine<20, double>(i * M_PI / N);
}
}
I was expecting that the compiler can generate a set of results for sine
and put them into out
without actually needing to compute the taylor series. Instead the generated code executes sine
as if it was any other non-constexpr
function.
My compiler is clang from Xcode 7.2 compiling with -O3
.
For a
constexpr
function to be evaluated at compile time the following must apply:The assignment in the
test
's for loop is not a constant expression. Consequently,sine
cannot be evaluated at compile time.What you really want, is to statically initialize the elements of an array using
sine()
. Using astd::array
and some helper machinery this is possible to do it as shown below:Live Demo