How useful are compile-time functions

compilerprogramming-languages

How useful are compile time functions? Personally I haven't worked in any language that supports them but they seem nifty in some cases.

For those who don't what I mean, a compile-time function is evaluated at compilation rather than at runtime,

so if you have for instance in your code

int i =   Pow(4,34);

you could instead do something like this

int i =   Pow<compilation>(4,34);

and the actual runtime code would be

 int i =   295147905179352825856;

Naturally there are some restrictions on what functions could be run at compile time, they pretty much have to be pure/static but I can also see some instances where it could be used to fetch data into something from a file or database (even though in most cases those things shouldn't be compiled into the code)

What do you think, is a feature like that actually useful or just "cool"? 🙂

Best Answer

I would expect most compilers to make this optimization anyway, certainly in release mode.

Letting the compiler do it is a useful feature for improving performance, I am not sure requiring the developer to flag the method in this way is that useful, compilers are better at this stuff than humans :-).

EDIT Reading the comments below has made me ponder this a little more and realize a couple of things... I am not a compiler expert and perhaps my assumption was a little optimisitc. I can see how it would indeed be hard for a compiler to automaticallly optimise out any non trivial function.