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"? :)

  • 1
    Isn't this what Lisp macros do? Lisp hackers certainly find them useful, and I've definitely had places where I wanted to do constructs like these in Java code (where the entire result couldn't be precalculated).
    – jprete
    Apr 11, 2011 at 15:19
  • 3
    You can do some amazing things with C++ templates. What sort of amazing things you actually should do is more open to question. Apr 11, 2011 at 17:11
  • I presume that this magic does not mean that stuff are reduced to numbers upon being calculated at compilation time so that at runtime these numbers are ready. But instead, what is produced is not necessarily a number, but instead an optimized machine code who is waiting for the remaining unknown inputs at runtime to do its calculation and produce a number.
    – eigenfield
    Apr 10, 2020 at 23:39

8 Answers 8


Let's say you're working on an application that have a loop that takes a lot of time because there is some calculations repeated.

Having compile-time evaluation of what you can in this loop can drastically reduce it's execution time. Because the "static" values are computed at compile-time, it is not computed at runtime, and only the values you can't guess at compile-time are really computed at runtime.

Anything that can be done before compile-time avoid you to have to do it at runtime, so it saves you time and memory at runtime. It's basically a powerful implicit optimization feature.

The other benefit is that, like in your example, you can still give name (functions or types) to values that would be hard to understand if were directly written where they were used. That's following the DRY principle and ease the reading.

By the way, in C++ we profit from compile-time code generation by templates. When we create a vector of object and use it's functions, the generated code will be :

  1. the minimal code necessary to do what you need to do with the vector;
  2. optimized at compile-time because the compiler will know a lot about the generated type, in witch context it's used, etc. It will help on performance and still use type-checking to make sure the generated code is "logic" - avoiding a lot of run-time errors that are not simple to debug.

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.

  • Now that you mention it, why wouldn't the compiler simply optimize stuff like that? Hard to come up with a scenario where you need to explicitly declare a compile-time function
    – Homde
    Apr 11, 2011 at 11:05
  • 4
    Strongly depends on the definition of Pow.
    – user1249
    Apr 11, 2011 at 15:15
  • I seem to remember a case where an optimizer was really cautious about floating-point calculations because a different floating-point model could be selected at runtime. Apr 11, 2011 at 17:12
  • Say there's a more complicated example, like there's a pure function constructing some sort of List with objects that will be used runtime and it's pretty demanding. Would that also be a thing that the compiler should optimize automatically? I can see a point where certain optimizeable methods are so demanding to calculate that you wouldn't want it done automatically...
    – Homde
    Apr 11, 2011 at 17:28
  • 2
    @Steve: I seriously doubt it. This is called SuperCompilation in Haskell (hackage.haskell.org/trac/ghc/wiki/Supercompilation) and it's definitely not easy. Languages that provide mechanisms (like C++ templates) are okay, but automatically detected whether this can be done or not is not trivial. Also, it only works with pure functions called with known arguments (obviously) and most languages (imperative) do not offer the possibility to mark whether a function is pure or not: with the definition out of the way, it's impossible for the compiler to apply this. Apr 11, 2011 at 19:29

When you combine compile time functions with string mixins in the D programming language, they're pretty useful. They allow you to generate arbitrary code at compile time in a way that's transparent to the API user and compile it just like normal code. This is of tremendous value for generative programming.


Modern compilers can evaluate static stuff like that at compile-time. For instance, x = 1 + 1 gets optimized by the compiler so the produced cpu instructions would be just x = 2.

A #define macro in C/C++ using only static data seems like a compile time function to me. Or using the inline keyword on a function often results in that as well.

And, yes, they are useful for performance reasons.


I would call it a optimization technique, and you are right that it can be applied to pure functions only. Generally, a function application

f a1 ... an

can be replaced by its result at compile time, if f is pure and the ai are constant expressions. Note that the function application is itself a constant expression.


There's an interesting trade-off involved here which I will raise and then leave open for your edification.

Compile-time functions make every build take longer, and execution of the product becomes quicker. Run-time evaluation is great when your programmers' time is expensive, and your users' time is cheap. Compile-time works best when it's the other way around.

  • 4
    Alternately, it could be disabled for debug versions and enabled for release versions. Apr 11, 2011 at 17:13
  • 1
    @David agreed, and that's how optimising compilers usually work. The approach proposed by @mko probably couldn't work like that.
    – user4051
    Apr 11, 2011 at 19:02

PL/I allows you to write macros in PL/I itself. Macros are simply PL/I procedures which are called during compile time and whose output is inserted into the source at the point of the call.


Cobol has several compile time options. For instance, you can have the compiler set all uninitialized variables to low-values, or you can have the compiler do nothing, and uninitialized variables would contain whatever was left over in memory from the previous execution. Some Cobol shops liked the low-value initialization, while some didn't. As you might imagine, it sometimes was a culture shock when a Cobol programmer moved from one shop to another.

Here's a list of Cobol compiler options for IBM mainframes.

  • Now convince the .NET-folks to upgrade :)
    – user1249
    Apr 11, 2011 at 15:16
  • @Thorbjørn Ravn Andersen: Well, I'd like to see them use .NET to write a process that takes the update transactions from the day and creates a new master transaction tape. :-) Oh, and it has to run at 8 pm Eastern time every weekday night. Apr 11, 2011 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.