I was very pleased to see the last example in Todd Veldhuisen’s metaprogram guide, where trigger functions like sin and cos are precomputed at compile time. Honestly, it blew me away, and if you write code that executes a huge number of these loops, like me, then this can significantly affect the performance increase.
Question 1
However, this made me wonder where the line is drawn between what is available as a runtime tool (by invoking the actual functions of the math library, such as sin or cos ), and what is available as soon as the compilation time is the math operator.
For example, Todd needs to manually calculate the trigger function using only regular arithmetic.
Is it possible to assume that the compiler is able to perform all the usual mathematical functions * + - / , but nothing more?
Question 2
In that case, you could get compile-time results for computing sin and cos for integers, right? That is, you cannot precompile the result of something like sin 45.5 , fix it?
Or, perhaps, if the template can only accept integers as parameters, you can take several integers and derive a float from it from the class, for example, pass 1 2 3 and do 1.23 get the value sin floating point value.
source share