I need to calculate the initial guess for the newtons method at compile-time. I'd like to prevent this calculation from happening at runtime.
Usually, I'd solve this problem using the enum trick:
enum
{
a = make_coeff(...),
b = make_coeff(...),
c = make_coeff(...)
};
where a, b, c are coefficients and make_coeff()
is a consteval/constexpr function. I can't use this approach this time, because the coefficients are of class type.
I have tried:
static constexpr coeff_type a[]{make_coeff(...), make_coeff(...), make_coeff(...)};
Would such an array of coefficients guarantee that make_coeff()
will not be executed at runtime? Even though make_coeff()
is marked consteval, gcc will happily generate code for it. But clang will not allow even a static constexpr array.
the errors clang gives are:
error: static variable not permitted in a constexpr function
error: cannot take address of consteval function 'make_coeff' outside of an immediate invocation
gcc is happy, but I am suspicious of what it is actually doing.
another thing I was thinking about is:
template <auto C>
constexpr auto coeff() noexcept
{
return C;
}
coeff<make_coeff(...)>();
since I can pass a coefficient as a non-type template parameter.
is a template array possible?
template <auto ...C>
decltype((C, ...)) const a[]{C...};
perhaps even something like:
template <std::size_t I, auto ...C>
auto get() noexcept
{
// ... easy to implement, but I'm not sure if it is worthwhile
}