What math operators are available in metaprogramming

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.

+6
source share
1 answer

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 math compilation time operator.

  • Named functions can only be used in compiletime, if they are declared constexpr , obey the rules of constexpr and call with compiletime constants.
  • Custom data types can be used in compiletime only if they are built using constexpr constructors from compiletime constants.
  • Any built-in statement that works with constant built-in compiletime types gives a compiletime constant.
  • Converting any type between inline expressions gives a compiletime constant if the original is a compiler constant.

Thus, it is not limited to four mathematical operators, you can use % and other operators, as well as template metafiles and constexpr expressions.

Question 2

In that case, you could get compile-time results for calculating sin and cos on integers, right? That is, you cannot precompile the result of something like sin 45.5, right?

Yes and no. In C ++ 03 you are limited to built-in and template metaphors, constexpr not available. Therefore, sin must be a template metaphone that can only work with integral constants, since floating point types are not allowed in templates. However, you can define patterns for the values ​​of fractions or fixed points and create a sin pattern for them. That would be quite tedious, and you could easily end the constraints of the template instance.

With C ++ 11, you can write constexpr functions that accept and work with floating point parameters.

+2
source

Source: https://habr.com/ru/post/945909/


All Articles