DEV Community

Discussion on: Let's try C++20 | std::is_constant_evaluated()

Collapse
 
dynamicsquid profile image
DynamicSquid

That's interesting! What would be an example of where you'd want a function to change depending on whether it's compile or runtime?

Collapse
 
pgradot profile image
Pierre Gradot • Edited

There is an example in the proposal to compute the power of a number.

There is a possible application case in my current project. We have a custom type for fixed-point numbers. We have a function to compute sine and cosine for this type, thanks to a lookup table of 1024 values. This is faster (to compute) but less accurate (especially if the table is small).

We may use std::is_constant_evaluated here:

  • at compile-time, we can use an accurate but slow algorithm (which we don't have yet)
  • at runtime-time, we can keep using our current algorithm